Jump to content

PhP and XML Help Please


vienem

Recommended Posts

Hey there,

 

Im new to the forum and PHP/XML - Been working on a script that I have no problems with until now.

It is probably a simple noob issue & I just know If anyone can help, they would be here ^^.

 

So here is the issue; I am Parsing data from and XML file BUT the XML file tree contains multiple strings with the same name; Example:-

 

<Movie>

    <Movie_Name>

    </Movie_Name>

</Movie>

<Movie>

    <Movie_Name>

    </Movie_Name>

</Movie>

 

As you can see if I call this in Php like so:-

echo $movie->Movie_Name;

It works perfectly but for only the very first Movie, How would I load the second lot of <Movie> in the tree?

 

I tried something called foreach and it kinda changes something lol but as I said Im rather new.

Link to comment
https://forums.phpfreaks.com/topic/189094-php-and-xml-help-please/
Share on other sites

Your need to loop the thought the elements or extract it like an arrary

 

I think an example would help.

<?php
$xmlstr = <<<XML
<XML>
<Movie>
     <Movie_Name>Movie 1</Movie_Name>
</Movie>
<Movie>
     <Movie_Name>Movie 2</Movie_Name>
</Movie>
</XML>
XML;
$xml = simplexml_load_string($xmlstr);

$movie = $xml->Movie;

echo "MOVIE ONE is:".$movie->Movie_Name;
echo "<HR />";

//array starts at 0 so this is item 2
echo "MOVIE TWO is:".$movie[1]->Movie_Name; 
echo "<HR />";

//Looping (okay the movie to movieS seams weird but i wanted to keep your variable the same!!)
//remember $movie is $xml->Movie
foreach ($movie as $movies) {
   echo $movies->Movie_Name, '<br />';
}

 

EDIT: added comment to loop

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.