Welcome to WebmasterWorld Guest from 54.162.57.225

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

xml parsing

     
12:29 pm on Apr 12, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 26, 2005
posts:92
votes: 0


Hello all, I need to parse some xml documents. I use Simple:XML and it works ok up to the point of matching elements that occur just once in my documents. How can I process something like:

<PubmedArticle>
<AuthorList CompleteYN="Y">
<Author ValidYN="Y">
<LastName>Wilde</LastName>
<ForeName>A</ForeName>
<Initials>A</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Reaves</LastName>
<ForeName>B</ForeName>
<Initials>B</Initials>
</Author>
<Author ValidYN="Y">
<LastName>Banting</LastName>
<ForeName>G</ForeName>
<Initials>G</Initials>
</Author>
</AuthorList>
</Article>
</PubmedArticle>

and print all Authors' names?

4:36 am on Apr 13, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


It would be something like this, which you have to include in a loop:

foreach($xml->Author as $author)
echo $author->LastName.'<br/>';
6:52 pm on Apr 13, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12541
votes: 1


</Article>

What's this? You should be getting an error in your tag mismatches.

After you fix that (remove it) you will have to go another node deeper in your xml object to get the Author information:

foreach($xml->AuthorList->Author as $author) { 
echo $author->LastName.'<br/>';
}