Jump to content

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

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.