作者:灵绾绾 | 来源:互联网 | 2014-03-29 12:37
高分在线求助XML解析本帖最后由HUENKE于2013-10-2515:58:22编辑<?xml version="1.0" encoding="utf-8"?><nodes><node id="
高分在线求助XML解析
本帖最后由 HUENKE 于 2013-10-25 15:58:22 编辑
type="0">
结构为 nodes下面有node下面还有node
我想做三重循环
foreach()
{
存公司名称
foreach()
{
存部门名称
foreach()
{ 存叶子名称}
}
}
请问我该怎么解析呢,
------解决方案--------------------你想要怎样的数据格式?,下面自己组织一下数据结构吧
$string = <<
type="0">
XML;
$xml = new SimpleXMLElement($string);
$result = $xml->xpath('//node/@name');
while(list( , $node) = each($result)) {
echo '@name: ',$node,"\n";
}
$result = $xml->xpath('//node/@id');
while(list( , $node) = each($result)) {
echo '@id: ',$node,"\n";
}
$result = $xml->xpath('//node[@type=1]/../@id');
while(list( , $node) = each($result)) {
echo '@部门的parent_id: ',$node,"\n";
}
$result = $xml->xpath('//node[@type=2]/../@id');
while(list( , $node) = each($result)) {
echo '@叶子的parent_id: ',$node,"\n";
}
?>
------解决方案--------------------$s =<<< XML
type="0">
XML;
$xml = simplexml_load_string($s);
foreach($xml->node as $node) {
foreach($node->attributes() as $k=>$v) echo "$k => $v\n";
foreach($node->node as $node) {
foreach($node->attributes() as $k=>$v) echo "$k => $v\n";