使用PHP重复XML解析

时间:2014-03-22 08:14:49

标签: php xml parsing

我之前问过,并获得了一个实例的答案,现在我必须将XML文件的多个重复实例解析为PHP变量; XML文件如下所示:

<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>

当我使用Xpath时,它可以正常工作,但只将第一个数据部分提取到变量中;

$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)');

并回显变量:

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

它只返回第一个值:

从上面的文件中提取所有数据的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

获取所有<client>节点然后循环它们是使dom遍历更清晰的好方法。以下是获取所有客户信息的示例

$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";
    }
  }  
}

答案 1 :(得分:0)

DOMXpath中使用的Xpath表达式,返回DOMNodelist,除非结果被转换为标量。因此,可以扩展示例以迭代节点列表。在循环内部,节点用作表达式的上下文。

$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)
  );
}

演示:https://eval.in/125083