| | |
How to Parse the Following XML Code...
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 36
Reputation:
Solved Threads: 0
I am trying to parse the the following xml code below, I need to retrieve only totalhits="376719" in the "resultset_web" child.
I also need to use code similar to the following:
/* XML TO BE PARSED */
I also need to use code similar to the following:
PHP Syntax (Toggle Plain Text)
<?php $doc = DOMDocument::loadXML($information); $result_node = $doc->getElementsByTagName('resultset_web'); $tHits = $result_node->item(0)->getAttribute('totalhits); ?>
/* XML TO BE PARSED */
PHP Syntax (Toggle Plain Text)
$information = '<ysearchresponse responsecode="200"> <prevpage> /ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&format=xml&count=1&start=0 </prevpage> <nextpage> /ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&format=xml&count=1&start=2 </nextpage> <resultset_web count="1" start="1" totalhits="376055" deephits="11600000"><result> <abstract> Home to the <b>seed</b> brand featuring products, nutrition facts, and more. </abstract> <clickurl> http://lrd.yahooapis.com/_ylc=X3oDMTRrYzhoc210BF9TAzIwMjMxNTI3MDIEYXBwaWQDZTRqMGRHZklrWTAuVm5QYWpfbThKaXZXRG1BZFdBVjUwdVRSdUlhcXZBLS0EcG9zAzEEc2VydmljZQNZU2VhcmNoV2ViBHNsawN0aXRsZQRzcmNwdmlkA2ZIUnh2RVBEQjJHQjIxOF9zZjhLc3dsa1RNTzJsa21qWS5FQUE2WkI-/SIG=10v00dabm/**http%3A//www.davidseeds.com/ </clickurl> <date>2008/12/12</date> <dispurl>www.<b>davidseeds.com</b></dispurl> <size>7122</size> <title>David <b>Sunflower</b> <b>Seeds</b></title> <url>http://www.davidseeds.com/</url> </result> </resultset_web> </ysearchresponse>'
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
php Syntax (Toggle Plain Text)
<?php $xYahooXML = ' <ysearchresponse responsecode="200"> <prevpage> /ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&format=xml&count=1&start=0 </prevpage> <nextpage> /ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&format=xml&count=1&start=2 </nextpage> <resultset_web count="1" start="1" totalhits="376055" deephits="11600000"> <result> <abstract> Home to the <b>seed</b> brand featuring products, nutrition facts, and more. </abstract> <clickurl> http://lrd.yahooapis.com/_ylc=X3oDMTRrYzhoc210BF9TAzIwMjMxNTI3MDIEYXBwaWQDZTRqMGRHZklrWTAuVm5QYWpfbThKaXZXRG1BZFdBVjUwdVRSdUlhcXZBLS0EcG9zAzEEc2VydmljZQNZU2VhcmNoV2ViBHNsawN0aXRsZQRzcmNwdmlkA2ZIUnh2RVBEQjJHQjIxOF9zZjhLc3dsa1RNTzJsa21qWS5FQUE2WkI-/SIG=10v00dabm/**http%3A//www.davidseeds.com/ </clickurl> <date>2008/12/12</date> <dispurl>www.<b>davidseeds.com</b></dispurl> <size>7122</size> <title>David <b>Sunflower</b> <b>Seeds</b></title> <url>http://www.davidseeds.com/</url> </result> </resultset_web> </ysearchresponse>'; $oSimpleXML = new SimpleXMLElement( $xYahooXML ); echo $oSimpleXML->resultset_web->attributes()->totalhits;
First, I used SimpleXML as it is generally easier to parse this kind of xml with.
Second, I don't know if the DOM will complain about this, but to my knowledge for XML to be valid a node can not contain an html entity that is not in entity format aka & => & Also, besides 5 entities quot, amp, apos, lt, gt any other element has to be numerically defined. http://en.wikipedia.org/wiki/List_of...ity_references
I had to change the &'s to & in the url's in your code to get SimpleXML to not complain about it being invalid xml etc.
Let me know if you have any additional questions.
If you're question/problem is solved don't forget to mark the thread as Solved!
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.11 or 5.3.0
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.11 or 5.3.0
•
•
Join Date: Mar 2008
Posts: 36
Reputation:
Solved Threads: 0
It turns out that the my initial code was working, but where the problem occurred was I forgot to add "<?xml version="1.0" encoding="utf-8"?>" to the beginning of the "$information" value.
Another mistake was I doing was not using a valid XML example. Solution for my inititial question. 3 Working Solutions Below!!
ALL THE BELOW WILL WORK
==================
SOLUTION 1
SOLUTION 2
SOLUTION 3 (SimpleXML) by mschroeder
Another mistake was I doing was not using a valid XML example. Solution for my inititial question. 3 Working Solutions Below!!
PHP Syntax (Toggle Plain Text)
$information = '<?xml version="1.0" encoding="utf-8"?> <ysearchresponse xmlns="http://www.inktomi.com/" responsecode="200"> <prevpage><![CDATA[/ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&format=xml&count=1&start=0]]></prevpage> <nextpage><![CDATA[/ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&format=xml&count=1&start=2]]></nextpage> <resultset_web count="1" start="1" totalhits="395561" deephits="12200000"> <result> <abstract><![CDATA[Home to the <b>seed</b> brand featuring products, nutrition facts, and more.]]></abstract> <clickurl>http://lrd.yahooapis.com/_ylc=X3oDMTRrOXJuZjk2BF9TAzIwMjMxNTI3MDIEYXBwaWQDZTRqMGRHZklrWTAuVm5QYWpfbThKaXZXRG1BZFdBVjUwdVRSdUlhcXZBLS0EcG9zAzEEc2VydmljZQNZU2VhcmNoV2ViBHNsawN0aXRsZQRzcmNwdmlkAzNuZzJiMFBEQjJIMl85N2RIXzZiUWdLMVF3cE1Xa21sV1NVQUJXamI-/SIG=10v00dabm/**http%3A//www.davidseeds.com/</clickurl> <date>2008/12/12</date> <dispurl><![CDATA[www.<b>davidseeds.com</b>]]></dispurl> <size>7122</size> <title><![CDATA[David <b>Sunflower</b> <b>Seeds</b>]]></title> <url>http://www.davidseeds.com/</url> </result> </resultset_web> </ysearchresponse>';
ALL THE BELOW WILL WORK
==================
SOLUTION 1
PHP Syntax (Toggle Plain Text)
<?php $doc = new DOMDocument; $doc->loadXML($information); $result_node = $doc->getElementsByTagName('resultset_web'); $tHits = $result_node->item(0)->getAttribute('totalhits'); ?>
SOLUTION 2
PHP Syntax (Toggle Plain Text)
<?php $doc = DOMDocument::loadXML($information); $result_node = $doc->getElementsByTagName('resultset_web'); $tHits = $result_node->item(0)->getAttribute('totalhits'); ?>
SOLUTION 3 (SimpleXML) by mschroeder
PHP Syntax (Toggle Plain Text)
<?php $oSimpleXML = new SimpleXMLElement( $information); echo $oSimpleXML->resultset_web->attributes()->totalhits; ?>
Last edited by peter_budo; Feb 25th, 2009 at 8:31 pm. Reason: Adding missing bracket in [/code] tag
![]() |
Similar Threads
- Read XML File in C/C++ (C++)
- Parse a XML element with AJAX (JavaScript / DHTML / AJAX)
- How to Parse XML in ASP? (ASP)
- How to Parse XML file (RSS, Web Services and SOAP)
- Need some help with parsing an XML file (PHP)
- Flash & XML: skipping past 2nd XML Declaration (Graphics and Multimedia)
- How to use xml? (HTML and CSS)
- Parse XML from ASP!!! (ASP)
Other Threads in the PHP Forum
- Previous Thread: Get footer down there!
- Next Thread: Text boxes and php
| Thread Tools | Search this Thread |
apache api array beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external fcc file files folder form forms forum freelancing function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction menu method mlm mod_rewrite multiple mysql neutrality oop pageing pagerank paypal pdf php phpmysql play problem query question radio random recursion remote root script search select server sessions sms soap source space sql support! syntax system table template tutorial update upload url validator variable video web youtube





