Jump to content

Recommended Posts

What would be the best way (easiest for noob) to parse this XML. I want to extract all the titles and put them in a list.

<?xml version="1.0" encoding="UTF-8"?>
<ytmnd_response timestamp="2009-11-09T21:01:33-06:00" request_type="user" extended_request="recent_sites" returned_results="1">
<users>
  <user user_id="1" user_name="max" signup_date="2004-04-06T18:36:06-05:00" last_active="2009-11-09T19:58:21-06:00">
   <recent_sites>
    <site site_id="902163" domain="comparingmyselftojesus" created="2009-02-23T22:24:05-06:00" modified="2009-02-25T01:01:16-06:00">
     <title>dying for your sins</title>
     <score computed_score="3.9563" total_votes="206" vote_sum="815" score_image="stars_red_40.gif"/>
    </site>
    <site site_id="897212" domain="MALLCOP" created="2009-01-15T03:48:42-06:00" modified="1969-12-31T18:00:00-06:00">
     <title>MALL COP WITH PAUL BLART</title>
     <moderated work_safe="true"/>
     <score computed_score="4.071" total_votes="310" vote_sum="1262" score_image="stars_red_41.gif"/>
    </site>
    <site site_id="846996" domain="fingersmoke" created="2008-02-18T13:58:25-06:00" modified="1969-12-31T18:00:00-06:00">
     <title>finger smoke</title>
     <moderated work_safe="true"/>
     <score computed_score="3.648" total_votes="375" vote_sum="1368" score_image="stars_red_36.gif"/>
    </site>
    <site site_id="796444" domain="trend" created="2007-08-02T06:08:01-05:00" modified="1969-12-31T18:00:00-06:00">
     <title>totally 100% trend</title>
     <moderated work_safe="true"/>
     <score computed_score="3.8403" total_votes="695" vote_sum="2669" score_image="stars_red_38.gif"/>
    </site>
    <site site_id="768866" domain="myrelationship" created="2007-05-25T22:49:37-05:00" modified="2007-07-01T21:38:08-05:00">
     <title>the captain     always goes down with his ship</title>
     <score computed_score="4.0567" total_votes="652" vote_sum="2645" score_image="stars_red_41.gif"/>
    </site>
   </recent_sites>
  </user>
</users>
</ytmnd_response>

 

I thought this XML stuff was easy but I'm having a heck of time getting it right.

Link to comment
https://forums.phpfreaks.com/topic/180935-solved-parsing-xml/
Share on other sites

Their is quite a few ways to do this, and it depends on that you wish to do with the data,

 

Now the XML path would be ytmnd_response / users / user / recent_sites / site / title

 

So you cab use xpath like this,

 

<?php
$XMLfile = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<ytmnd_response timestamp="2009-11-09T21:01:33-06:00" request_type="user" extended_request="recent_sites" returned_results="1">
<users>
  <user user_id="1" user_name="max" signup_date="2004-04-06T18:36:06-05:00" last_active="2009-11-09T19:58:21-06:00">
   <recent_sites>
    <site site_id="902163" domain="comparingmyselftojesus" created="2009-02-23T22:24:05-06:00" modified="2009-02-25T01:01:16-06:00">
     <title>dying for your sins</title>
     <score computed_score="3.9563" total_votes="206" vote_sum="815" score_image="stars_red_40.gif"/>
    </site>
    <site site_id="897212" domain="MALLCOP" created="2009-01-15T03:48:42-06:00" modified="1969-12-31T18:00:00-06:00">
     <title>MALL COP WITH PAUL BLART</title>
     <moderated work_safe="true"/>
     <score computed_score="4.071" total_votes="310" vote_sum="1262" score_image="stars_red_41.gif"/>
    </site>
    <site site_id="846996" domain="fingersmoke" created="2008-02-18T13:58:25-06:00" modified="1969-12-31T18:00:00-06:00">
     <title>finger smoke</title>
     <moderated work_safe="true"/>
     <score computed_score="3.648" total_votes="375" vote_sum="1368" score_image="stars_red_36.gif"/>
    </site>
    <site site_id="796444" domain="trend" created="2007-08-02T06:08:01-05:00" modified="1969-12-31T18:00:00-06:00">
     <title>totally 100% trend</title>
     <moderated work_safe="true"/>
     <score computed_score="3.8403" total_votes="695" vote_sum="2669" score_image="stars_red_38.gif"/>
    </site>
    <site site_id="768866" domain="myrelationship" created="2007-05-25T22:49:37-05:00" modified="2007-07-01T21:38:08-05:00">
     <title>the captain     always goes down with his ship</title>
     <score computed_score="4.0567" total_votes="652" vote_sum="2645" score_image="stars_red_41.gif"/>
    </site>
   </recent_sites>
  </user>
</users>
</ytmnd_response>

EOD;

$xml = new SimpleXMLElement($XMLfile);
$result = $xml->xpath('/ytmnd_response/users/user/recent_sites/site/title');
while(list( , $node) = each($result)) {
    echo "$node\n";
}

 

hope that helps (3:30am.. I'm off to sleep)

Link to comment
https://forums.phpfreaks.com/topic/180935-solved-parsing-xml/#findComment-954558
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.