Forum Moderators: coopster

Message Too Old, No Replies

xml parsing

         

ktsirig

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

10+ Year Member



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?

eelixduppy

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



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

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

coopster

6:52 pm on Apr 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



</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/>';
}