高分在线求助XML解析
本帖最后由 HUENKE 于 2013-10-25 15:58:22 编辑
结构为 nodes下面有node下面还有node
我想做三重循环
foreach()
{
存公司名称
foreach()
{
存部门名称
foreach()
{ 存叶子名称}
}
}
请问我该怎么解析呢,
type="0">
结构为 nodes下面有node下面还有node
我想做三重循环
foreach()
{
存公司名称
foreach()
{
存部门名称
foreach()
{ 存叶子名称}
}
}
请问我该怎么解析呢,
xml
分享到:
------解决方案--------------------
你想要怎样的数据格式?,下面自己组织一下数据结构吧
------解决方案--------------------
------解决方案--------------------
你想要怎样的数据格式?,下面自己组织一下数据结构吧
$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 =
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";









