繁体   English   中英

用PHP重复XML解析

[英]Repeating XML parsing with PHP

我之前问过,并得到了一个实例的答案,现在我必须将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;

它仅返回第一个值:

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

获取所有<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";
    }
  }  
}

除非结果强制转换为标量,否则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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM