时间:2021-07-01 10:21:17 帮助过:13人阅读
xml
SimpleXML 用过嘛?
SimpleXML 没解析出来
能帮简单调试一下吗
你想要怎样的数据格式?,下面自己组织一下数据结构吧
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 =<<< XMLid => c5569e2c-0f4a-4ec2-86ba-736b79eca18cXML;$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"; foreach($node->node as $node) { foreach($node->attributes() as $k=>$v) echo "$k => $v\n"; } }}
就是按他原样的树状结构
如
A公司
部门1
叶子1
部门2
。。。。
再给你一个吧,自己修改一下就是树状了
XML;$xml = new SimpleXMLElement($string);// var_export($xml);$xmlIterator = new RecursiveIteratorIterator( new SimpleXMLIterator($string), RecursiveIteratorIterator::SELF_FIRST);foreach ($xmlIterator as $nodeName => $node) { echo str_repeat('-', (int)$node['type']); echo $node['id'], ',', $node['name'], PHP_EOL;}?>
上面#6
$xml = new SimpleXMLElement($string);
这句可以删掉,前面调试的时候留下的