Page 1 of 1
Parse XML from PHP Output
Posted: Wed Mar 12, 2008 8:46 am
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:
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:
Posting Code in the Forums to learn how to do it too.
Re: Parse XML from PHP Output
Posted: Wed Mar 12, 2008 9:48 am
by pickle
You could try include()ing the file & capturing the output with output buffering.
Re: Parse XML from PHP Output
Posted: Wed Mar 12, 2008 11:01 am
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.
Re: Parse XML from PHP Output
Posted: Wed Mar 12, 2008 11:14 am
by pickle
You could maybe try
file_get_contents() or some other file opening methods.
Re: Parse XML from PHP Output
Posted: Wed Mar 12, 2008 11:22 am
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.
Re: Parse XML from PHP Output
Posted: Wed Mar 12, 2008 11:24 am
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!
Re: Parse XML from PHP Output
Posted: Wed Mar 12, 2008 1:39 pm
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.
Re: Parse XML from PHP Output
Posted: Wed Mar 12, 2008 1:56 pm
by RobertGonzalez
Re: Parse XML from PHP Output
Posted: Wed Mar 12, 2008 2:12 pm
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).