Parse XML from PHP Output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stoneferry
Forum Newbie
Posts: 9
Joined: Sat Feb 16, 2008 10:39 am

Parse XML from PHP Output

Post by stoneferry »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi, using PHP I'd like to know how you would be able to parse XML output from an external PHP document. The parsing as such is not a problem, the issue is getting the actual output from the .PHP.

The output is output to the browser window in a flat format:

Code: Select all

<a>
    <b>
        <c1>value</c1>
        <c2>value</c2>        
        <c3>value</c3>        
    </b>
    <b>
        <c1>value</c1>
        <c2>value</c2>        
        <c3>value</c3>        
    </b>
</a>
How do achieve this within PHP?

Many thanks.


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Parse XML from PHP Output

Post by pickle »

You could try include()ing the file & capturing the output with output buffering.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
stoneferry
Forum Newbie
Posts: 9
Joined: Sat Feb 16, 2008 10:39 am

Re: Parse XML from PHP Output

Post by stoneferry »

Hi, and thanks for your response the file cannot be include()ed due to permission issues. It just outputs the XML formatted data formatted above.

It is a third party .php file for which there are no permissions.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Parse XML from PHP Output

Post by pickle »

You could maybe try file_get_contents() or some other file opening methods.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Parse XML from PHP Output

Post by RobertGonzalez »

If the file is on another server then you are going to turn on allow_url_fopen in your php.ini. This is for include() and file_get_contents().

What exactly are you after here? Do you simply want to get the XML output and use that to traverse its DOM? SimpleXML might be a good place to start.
radicalfix
Forum Newbie
Posts: 7
Joined: Mon Dec 04, 2006 10:49 pm

Re: Parse XML from PHP Output

Post by radicalfix »

Stoneferry,

I can't get what you really mean, but let me try.

The most simple way is by using SimpleXML, which is a new extension in PHP5. If you are using version 5, this should not be a problem:

Code: Select all

 
<?php
$string = <<<XML
 <a>
    <b>
        <c1>value</c1>
        <c2>value</c2>        
        <c3>value</c3>        
    </b>
    <b>
        <c1>value</c1>
        <c2>value</c2>        
        <c3>value</c3>        
    </b>
</a>
XML;
 
$xml = new SimpleXMLElement($string);
 
echo $xml->asXML(); 
 
?>
 
You can also put the XML into a a PHP page, let say xml.php:

Code: Select all

 
<?php
$string = <<<XML
 <a>
    <b>
        <c1>value</c1>
        <c2>value</c2>        
        <c3>value</c3>        
    </b>
    <b>
        <c1>value</c1>
        <c2>value</c2>        
        <c3>value</c3>        
    </b>
</a>
XML;
?>
 
Then include it at the top of the page.

Code: Select all

 
<?php
 
include 'xml.php';
 
$xml = new SimpleXMLElement($string);
 
echo $xml->asXML(); // 
 
?>
 
Hope it helps!
stoneferry
Forum Newbie
Posts: 9
Joined: Sat Feb 16, 2008 10:39 am

Re: Parse XML from PHP Output

Post by stoneferry »

Hi, firstly please note this is a third party XML and no changes can be made to it.

As for the answer, perhaps I've not explained myself correctly. I'll try again. I can't show the xml output in question, although to give an idea, please see the below website which outputs an XML document relating to the weather where zip code is an input variable in the URL:

http://xml.weather.yahoo.com/forecastrss?p=12345

Let's pretend that output is not formatted and it just displays the raw XML:

Code: Select all

<a>    <b>        <c1>value</c1>        <c2>value</c2>                <c3>value</c3>            </b>    <b>        <c1>value</c1>        <c2>value</c2>                <c3>value</c3>            </b></a>
That's all I want. I want that output XML parsing, and formatting into a string. I'd know how to do this if it was just an xml file, however for the sake of doing things properly that's what brings me to this forum for your help.


Thanks in advance

PS: Sorry if the XML code doesn't display properly again, I tried using the suggested tag.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Parse XML from PHP Output

Post by RobertGonzalez »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Parse XML from PHP Output

Post by RobertGonzalez »

In fact, here is some code that I know works (I just tested it out on my system):

Code: Select all

<?php
$xml = file_get_contents('http://xml.weather.yahoo.com/forecastrss?p=94536');
$parser = new SimpleXMLElement($xml);
echo '<pre>', print_r($parser, 1), '</pre>';
?>
This requires PHP 5 (or a SimpleXML clone of some sort - there are plenty of them out there).
Post Reply