arcdegree
arcdegree

Reputation: 2720

How can I parse this XML with SimpleXML for PHP?

I'm attempting to parse an XML document, but I am having trouble because I think the XML is not formatted in a way that works easily with SimpleXML. Some elements in the XML can have 0 or more elements and I don't know how to use SimpleXML to pull the data out properly. It appears that SimpleXML "clumps" together elements since the key names in this document are datatypes. I've created a simplified example.

<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<results>
  <result kind="Host">
    <headings>
        <heading>Id</heading>
        <heading>Name</heading>
        <heading>IP Addresses</heading>
        <heading>DNS</heading>
        <heading>Timestamp</heading>
        <heading>Type</heading>
    </headings>
    <row>
        <string>38209387</string>
        <string>johnson38</string>
        <string>192.168.1.1</string>
        <string>joe.example.com</string>
        <datetime>Wed Sep  4 22:13:02 2009</datetime>
        <void/>
    </row>
    <row>
        <string>8283324</string>
        <string>smith42</string>
        <list>
            <string>192.168.1.7</string>
            <string>192.168.1.8</string>
        </list>
        <list>
            <string>john.example.com</string>
            <string>nick.example.com</string>
        </list>
        <datetime>Wed Oct  4 12:13:02 2009</datetime>
        <string>Major Server</string>
    </row>
  </result>
</results>
XML;


$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->result->row as $row) {
    echo "<pre>";
    print_r($row);
    echo "</pre>";

    //display the 1st IP Address
    echo "IP Address: ".$row->string[2]."<br />";
}
?>

Output

 SimpleXMLElement Object
(
    [string] => Array
        (
            [0] => 38209387
            [1] => johnson38
            [2] => 192.168.1.1
            [3] => joe.example.com
        )

    [datetime] => Wed Sep  4 22:13:02 2009
    [void] => SimpleXMLElement Object
        (
        )

)
IP Address: 192.168.1.1
SimpleXMLElement Object
(
    [string] => Array
        (
            [0] => 8283324
            [1] => smith42
            [2] => Major Server
        )

    [list] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [string] => Array
                        (
                            [0] => 192.168.1.7
                            [1] => 192.168.1.8
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [string] => Array
                        (
                            [0] => john.example.com
                            [1] => nick.example.com
                        )

                )

        )

    [datetime] => Wed Oct  4 12:13:02 2009
)
IP Address: Major Server

Upvotes: 1

Views: 668

Answers (1)

Wrikken
Wrikken

Reputation: 70460

$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->result->row as $row) {
    //this will still have elements in order, so you'll have to count:
    $count = 1;
    foreach($row->children() as $child){
       //whatever you want.
       $count++
    }
    //if you only want ip (the 3rd node):
    var_dump($row->xpath('*[3]'));
}

Upvotes: 2

Related Questions