用PHP重复XML解析
我以前问过,得到了一个实例的答案,现在我必须将XML文件的多个重复实例解析为PHP变量;XML文件如下所示:用PHP重复XML解析,php,xml,parsing,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=""></r
<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>
和回音变量:
echo $client_type;
echo $name ;
echo $conn_ip;
它只返回第一个值:
从上面的文件中提取所有数据的最佳方法是什么?获取所有
节点,然后在其上循环是一种使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)
);
}
演示:
$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)
);
}