Jump to content

Parsing the xml


bslone12

Recommended Posts

I am trying to figure this out. I have a folder of xml files I need to parse and each one has a ton of entries so I will need a foreach statement I assume, but can't seem to get it figured out. each xml in the folder has multiples of these entries.

<?xml version="1.0" encoding="UTF-8"?>

-<urlset xmlns:hulu="http://www.hulu.com/schemas/sitemap-hulu/1.0" xmlns:hulu-video="http://www.hulu.com/schemas/sitemap-hulu-video/1.0" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.0">


-<url>

<loc>http://www.hulu.com/watch/867547</loc>


-<video:video>

<video:thumbnail_loc>http://ib2.huluim.com/video/60644813?size=220x124</video:thumbnail_loc>

<video:title>Popcorn With Peter Travers: Brie Larson: How Real Life-Events Inspired Her Powerful Performance</video:title>

<video:description>Larson appears on "Popcorn with Peter Travers" to talk about her new film "Room" and how her own childhood set the stage for her newest role.</video:description>

<video:family_friendly>Yes</video:family_friendly>

<video:duration>1041.24</video:duration>

<video:rating>3</video:rating>

<video:publication_date>2015-11-06T05:00:00Z</video:publication_date>

<hulu-video:season_number>8</hulu-video:season_number>

<video:season_number>8</video:season_number>

<hulu-video:episode_number>53</hulu-video:episode_number>

<video:episode_number>53</video:episode_number>

<hulu-video:video_type>episode</hulu-video:video_type>

<video:video_type>episode</video:video_type>

<hulu-video:content_id>60644813</hulu-video:content_id>

<hulu-video:show_id>4954</hulu-video:show_id>

<hulu-video:air_date>2015-11-06T00:00:00Z</hulu-video:air_date>

<video:air_date>2015-11-06T00:00:00Z</video:air_date>

<video:added_date>2015-11-06T05:00:00Z</video:added_date>

<hulu-video:company>abc-news</hulu-video:company>

<video:company>abc-news</video:company>

<hulu:seriesDescription>Rolling Stone's Peter Travers holds in-depth and detailed talks with movie stars and moviemakers on their big-budget mega-films, their smaller personal projects and their lives and times. AND see a musical interlude from each!</hulu:seriesDescription>

<hulu:mediaType>TV</hulu:mediaType>

<hulu:channel>News and Information</hulu:channel>

<video:requires_subscription>no</video:requires_subscription>


-<video:tvshow>

<video:show_title>Popcorn With Peter Travers</video:show_title>

<hulu-video:show_id>4954</hulu-video:show_id>

<video:video_type>Full</video:video_type>

<video:episode_title>Brie Larson: How Real Life-Events Inspired Her Powerful Performance</video:episode_title>

<video:season_number>8</video:season_number>

<video:episode_number>53</video:episode_number>

<video:premier_date>2015-11-06T00:00:00Z</video:premier_date>

</video:tvshow>

</video:video>

</url>


I want to pull the following field

<loc>http://www.hulu.com/watch/867547</loc>

for all of the entries in all of the xml files in that folder.  thats it for now, how can I do this?

Link to comment
Share on other sites

I was testing with just one of xml files in the folder, then i was going to try to get it to do all, i was able to get the loc to echo with the following code

<?php
    $source = 'video_data/0.xml';

 // load as string
 $xmlstr = file_get_contents($source);
 $xmlcont = new SimpleXMLElement($xmlstr);
 foreach($xmlcont as $url) 
 {
    echo $url->loc;
    echo "<br>"  ;
 }
 
?> 

how do I then exand it to every xml in the folder? also if I wanted to get more of the fields, such as the ones under <video:video><video:title> and <video:video><video:thumbnail:loc> how can I do that. I tried to echo $url->video:video->video:title but got nothing, this is my first time parsing an xml obviously, thanks for the help.

Link to comment
Share on other sites

For all the files, you'll need another loop: use glob to get the list of files you want, foreach over it as $source, and then put that code you have to load a file into it.

 

Dealing with un-namespaced XML elements (like ) is easy: do what you're doing now. Dealing with namespaced elements (like ) takes a bit more work.

 

To do something like ->video:title, which you can't do for a couple different reasons, you need to use ::children.

const XMLNS_VIDEO = "http://www.google.com/schemas/sitemap-video/1.0"; // <urlset xmlns:video="...">
echo $url->children(XMLNS_VIDEO)->video->title;
Explanation? When you do ->video, SimpleXML will try to find a
Link to comment
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.