943,883 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1341
  • PHP RSS
Feb 24th, 2009
0

How to Parse the Following XML Code...

Expand Post »
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:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $doc = DOMDocument::loadXML($information);
  3. $result_node = $doc->getElementsByTagName('resultset_web');
  4. $tHits = $result_node->item(0)->getAttribute('totalhits);
  5. ?>
  6.  

/* XML TO BE PARSED */
PHP Syntax (Toggle Plain Text)
  1. $information = '<ysearchresponse responsecode="200">
  2. <prevpage>
  3. /ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&format=xml&count=1&start=0
  4. </prevpage>
  5. <nextpage>
  6. /ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&format=xml&count=1&start=2
  7. </nextpage>
  8. <resultset_web count="1" start="1" totalhits="376055" deephits="11600000"><result>
  9. <abstract>
  10. Home to the <b>seed</b> brand featuring products, nutrition facts, and more.
  11. </abstract>
  12. <clickurl>
  13. http://lrd.yahooapis.com/_ylc=X3oDMTRrYzhoc210BF9TAzIwMjMxNTI3MDIEYXBwaWQDZTRqMGRHZklrWTAuVm5QYWpfbThKaXZXRG1BZFdBVjUwdVRSdUlhcXZBLS0EcG9zAzEEc2VydmljZQNZU2VhcmNoV2ViBHNsawN0aXRsZQRzcmNwdmlkA2ZIUnh2RVBEQjJHQjIxOF9zZjhLc3dsa1RNTzJsa21qWS5FQUE2WkI-/SIG=10v00dabm/**http%3A//www.davidseeds.com/
  14. </clickurl>
  15. <date>2008/12/12</date>
  16. <dispurl>www.<b>davidseeds.com</b></dispurl>
  17. <size>7122</size>
  18. <title>David <b>Sunflower</b> <b>Seeds</b></title>
  19. <url>http://www.davidseeds.com/</url>
  20. </result>
  21. </resultset_web>
  22. </ysearchresponse>'
Similar Threads
Reputation Points: 20
Solved Threads: 0
Light Poster
lonestar23 is offline Offline
43 posts
since Mar 2008
Feb 24th, 2009
2

Re: How to Parse the Following XML Code...

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $xYahooXML = '
  4. <ysearchresponse responsecode="200">
  5. <prevpage> /ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&amp;format=xml&amp;count=1&amp;start=0 </prevpage>
  6. <nextpage> /ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&amp;format=xml&amp;count=1&amp;start=2 </nextpage>
  7. <resultset_web count="1" start="1" totalhits="376055" deephits="11600000">
  8. <result>
  9. <abstract> Home to the <b>seed</b> brand featuring products, nutrition facts, and more. </abstract>
  10. <clickurl> http://lrd.yahooapis.com/_ylc=X3oDMTRrYzhoc210BF9TAzIwMjMxNTI3MDIEYXBwaWQDZTRqMGRHZklrWTAuVm5QYWpfbThKaXZXRG1BZFdBVjUwdVRSdUlhcXZBLS0EcG9zAzEEc2VydmljZQNZU2VhcmNoV2ViBHNsawN0aXRsZQRzcmNwdmlkA2ZIUnh2RVBEQjJHQjIxOF9zZjhLc3dsa1RNTzJsa21qWS5FQUE2WkI-/SIG=10v00dabm/**http%3A//www.davidseeds.com/ </clickurl>
  11. <date>2008/12/12</date>
  12. <dispurl>www.<b>davidseeds.com</b></dispurl>
  13. <size>7122</size>
  14. <title>David <b>Sunflower</b> <b>Seeds</b></title>
  15. <url>http://www.davidseeds.com/</url>
  16. </result>
  17. </resultset_web>
  18. </ysearchresponse>';
  19.  
  20. $oSimpleXML = new SimpleXMLElement( $xYahooXML );
  21. 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 & => &amp; 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 &amp; 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.
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008
Feb 24th, 2009
0

Re: How to Parse the Following XML Code...

Your method worked perfectly, but like you said, after converting everything "&" to "&amp;" etc... I will wait till early tomorrow see if anyone else posts an answer to "parsing" using DOM. Reason being this will be dynamic code being plugged in and it needs work without any conversion.
Reputation Points: 20
Solved Threads: 0
Light Poster
lonestar23 is offline Offline
43 posts
since Mar 2008
Feb 25th, 2009
0

Re: How to Parse the Following XML Code...

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!!

PHP Syntax (Toggle Plain Text)
  1. $information = '<?xml version="1.0" encoding="utf-8"?>
  2. <ysearchresponse xmlns="http://www.inktomi.com/" responsecode="200">
  3. <prevpage><![CDATA[/ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&format=xml&count=1&start=0]]></prevpage>
  4. <nextpage><![CDATA[/ysearch/web/v1/sunflower%20seeds?appid=e4j0dGfIkY0.VnPaj_m8JivWDmAdWAV50uTRuIaqvA--&format=xml&count=1&start=2]]></nextpage>
  5. <resultset_web count="1" start="1" totalhits="395561" deephits="12200000">
  6. <result>
  7. <abstract><![CDATA[Home to the <b>seed</b> brand featuring products, nutrition facts, and more.]]></abstract>
  8. <clickurl>http://lrd.yahooapis.com/_ylc=X3oDMTRrOXJuZjk2BF9TAzIwMjMxNTI3MDIEYXBwaWQDZTRqMGRHZklrWTAuVm5QYWpfbThKaXZXRG1BZFdBVjUwdVRSdUlhcXZBLS0EcG9zAzEEc2VydmljZQNZU2VhcmNoV2ViBHNsawN0aXRsZQRzcmNwdmlkAzNuZzJiMFBEQjJIMl85N2RIXzZiUWdLMVF3cE1Xa21sV1NVQUJXamI-/SIG=10v00dabm/**http%3A//www.davidseeds.com/</clickurl>
  9. <date>2008/12/12</date>
  10.  
  11. <dispurl><![CDATA[www.<b>davidseeds.com</b>]]></dispurl>
  12. <size>7122</size>
  13. <title><![CDATA[David <b>Sunflower</b> <b>Seeds</b>]]></title>
  14. <url>http://www.davidseeds.com/</url>
  15. </result>
  16. </resultset_web>
  17. </ysearchresponse>';

ALL THE BELOW WILL WORK
==================
SOLUTION 1
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $doc = new DOMDocument;
  3. $doc->loadXML($information);
  4. $result_node = $doc->getElementsByTagName('resultset_web');
  5. $tHits = $result_node->item(0)->getAttribute('totalhits');
  6. ?>

SOLUTION 2
PHP Syntax (Toggle Plain Text)
  1. <?php $doc = DOMDocument::loadXML($information);
  2. $result_node = $doc->getElementsByTagName('resultset_web');
  3. $tHits = $result_node->item(0)->getAttribute('totalhits');
  4. ?>

SOLUTION 3 (SimpleXML) by mschroeder
PHP Syntax (Toggle Plain Text)
  1. <?php $oSimpleXML = new SimpleXMLElement( $information);
  2. echo $oSimpleXML->resultset_web->attributes()->totalhits;
  3. ?>
Last edited by peter_budo; Feb 25th, 2009 at 8:31 pm. Reason: Adding missing bracket in [/code] tag
Reputation Points: 20
Solved Threads: 0
Light Poster
lonestar23 is offline Offline
43 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Get footer down there!
Next Thread in PHP Forum Timeline: Text boxes and php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC