Using SimpleXML to extract data : simplexml_load_string « XML « PHP






Using SimpleXML to extract data

 
$sx = simplexml_load_file('address-book.xml');

foreach ($sx->person as $person) {
    $firstname_text_value = $person->firstname;
    $lastname_text_value = $person->lastname;
    
    print "$firstname_text_value $lastname_text_value\n";
}

//
<?xml version="1.0"?>
<address-book>
    <person id="1">
        <firstname>D</firstname>
        <lastname>S</lastname>
        <city>New York</city>
        <state>NY</state>
        <email>s@php.net</email>
    </person>

    <person id="2">
        <firstname>A</firstname>
        <lastname>T</lastname>
        <city>San Francisco</city>
        <state>CA</state>
        <email>a@php.net</email>
    </person>
</address-book>
  
  








Related examples in the same category

1.Accessing identically named elements
2.Adding attributes
3.Changing elements and attributes
4.Loop through children
5.Loading an XML document from a file
6.Fetching a remote XML document
7.Parsing XML in a string
8.Parsing an XML Document with SimpleXML
9.simplexml_load_file.php
10.Loading a remote XML document
11.Outputting XML
12.Simple XML
13.Print XML element attributes
14.Printing sub-element contents
15.Saving an XML document to a file