高分線上求助XML解析
本帖最後由 HUENKE 於 2013-10-25 15:58:22 編輯
type="0">
結構為 nodes下面有node下面還有node
我想做三重迴圈
foreach()
{
存公司名稱
foreach()
{
存部門名稱
foreach()
{ 存葉子名稱}
}
}
請問我該怎麼解析呢, xml
分享到:
------解決方案--------------------
你想要怎樣的資料格式?,下面自己組織一下資料結構吧
$string = <<
type="0">
XML;
$xml = new SimpleXMLElement($string);
$result = $xml->xpath([email protected]');
while(list( , $node) = each($result)) {
echo [email protected]: ',$node,"\n";
}
$result = $xml->xpath([email protected]');
while(list( , $node) = each($result)) {
echo [email protected]: ',$node,"\n";
}
$result = $xml->xpath('//node[@type=1]/..[email protected]');
while(list( , $node) = each($result)) {
echo [email protected]_id: ',$node,"\n";
}
$result = $xml->xpath('//node[@type=2]/..[email protected]');
while(list( , $node) = each($result)) {
echo [email protected]_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";