extracting multiple tags from xml using PHP

Posted by user1479431 on Stack Overflow See other posts from Stack Overflow or by user1479431
Published on 2012-07-08T21:12:27Z Indexed on 2012/07/08 21:15 UTC
Read the original article Hit count: 170

Filed under:
|

Here is my address.xml

<?xml version="1.0" ?>
<!--Sample XML document -->
<AddressBook>
    <Addressentry>
        <firstName>jack</firstName>
        <lastName>S</lastName>
        <Address>2899,Ray Road</Address>
        <Email>[email protected]</Email>
    </Addressentry>
    <Addressentry>
        <firstName>Sid</firstName>
        <lastName>K</lastName>
        <Address>238,Baseline Road,TX</Address>
        <Email>[email protected]</Email>
        <Email>[email protected]</Email>
    </Addressentry>
    <Addressentry>
        <firstName>Satya</firstName>
        <lastName>Yar</lastName>
        <Address>6,Rural Road,Tempe,AZ</Address>
        <Email>[email protected]</Email>
       <Email>[email protected]</Email>
        <Email>[email protected]</Email>
    </Addressentry>

</AddressBook>

I am trying to load all the entries using PHP code as below. Each addressentry can have one or more tags. Right now from the code below I am able to extract only one tag. My question is how do I extract all tags associated with particular Addressentry. that is I want to print all emails on the same line.

<?php
$theData = simplexml_load_File("address.xml");

foreach($theData->Addressentry as $theAddress) {
    $theFirstName = $theAddress->firstName;
    $theLastName = $theAddress->lastName;
    $theAdd = $theAddress->Address;


    echo "<p>".$theFirstName."
    ".$theLastName."<br/>
    ".$theAdd."<br/>
    ".$theAddress->Email."<br/>
    </p>";


    unset($theFirstName);
    unset($theLastName);
    unset($theAdd);
    unset($theEmail);
}
?>

Any help would be appreciated

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml