Repeating XML parsing with PHP

616

I asked before, and got the answers for one instance, now i have to parse multiple repeating instances of XML file into PHP variables; XML file looks like this:

<status>
  <client type="s" name="root" desc="" protocol="server" protocolext="" au="0" thid="0x15e9190">
     <request="0000" srvid="0000" time="" history="" answered=""></request>
     <times login="2013-04-16T10:59:16+0200" online="7001" idle="0"></times>
     <connection ip="127.0.0.1" port="0">OK</connection>
  </client>
  <client type="p" name="user1" desc="" protocol="run1" protocolext="" au="-1" thid="0x15f1790">
     <request="0000" srvid="0000" time="" history="2667" answered=""></request>
     <times login="2013-04-16T10:59:16+0200" online="7001" idle="6999"></times>
     <connection ip="127.0.2.2" port="10002">CONNECTED</connection>
  </client>
  <client type="p" name="user2" desc="" protocol="run2" protocolext="" au="-1" thid="0x15f32b0">
     <request="0000" srvid="0000" time="" history="" answered=""></request>
     <times login="2013-04-16T10:59:16+0200" online="7001" idle="7001"></times>
     <connection ip="127.0.3.1" port="12001">CONNECTED</connection>
  </client>
  <client type="p" name="user3" desc="" protocol="run1" protocolext="" au="-1" thid="0x1631170">
     <request="0000" srvid="0000" time="" history="" answered=""></request>
     <times login="2013-04-16T10:59:16+0200" online="7001" idle="7001"></times>
     <connection ip="127.0.4.1" port="9600">CONNECTED</connection>
  </client>
</status>

when i do it withXpath, it works OK, but fetches only first data part into variables;

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

$client_type = $xpath->evaluate('string(/status/client/@type)'); 
$name = $xpath->evaluate('string(/status/client/@name)');; 
$conn_ip = $xpath->evaluate('string(/status/client/connection/@ip)');

and echoing variables:

echo $client_type;
echo $name ;
echo $conn_ip;

it returns only first values:

What would be the best way to pull ALL DATA from the file above?

58

Answer

Solution:

Getting all the<client> nodes and then looping over them is a good way to make the dom traversal clearer. Here is an example of getting all the clients infos

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

// use the double // to find ALL clients in the document
$clientXpath = "//client";
$clients = $xpath->evaluate($clientXpath);


// foreach client node
foreach ($clients as $ii=>$client) {
  // get the type attribute of client node
  echo $client->getAttribute('type') . "\n";
  // get the name attribute of client node
  echo $client->getAttribute('name') . "\n";

  // get clients children
  $children = $client->childNodes;
  foreach ($children as $child) {
    // ignore textnodes
    if ($child instanceof DomText) {
      continue;
    }

    // now concern ourself only with the connection tag, which
    // contains the ip
    if ($child->tagName == 'connection') {
      print $child->getAttribute('ip') . "\n";
    }
  }  
}
921

Answer

Solution:

Xpath expression used in DOMXpath, return a DOMNodelist unless the result is casted to a scalar. So the example can be expanded to iterate over a list of nodes. Inside the loop the node is used as a context for the expressions.

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

foreach ($xpath->evaluate('/status/client') as $client) {
  var_dump(
    $xpath->evaluate('string(@type)', $client),
    $xpath->evaluate('string(@name)', $client), 
    $xpath->evaluate('string(connection/@ip)', $client)
  );
}

Demo: https://eval.in/125083

People are also looking for solutions to the problem: php - Meta tags allowed under head section?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.