我正在使用XML DOM和PHP构建脚本
$troops = $xmlfile->getElementsByTagName("troops");
foreach( $troops as $troop ) {
$sniper = $troop->getElementsByTagName( "sniper" );
$otroop[0] = $sniper->item(0)->nodeValue;
$riffle = $troop->getElementsByTagName( "riffle" );
$otroop[1] = $riffle->item(0)->nodeValue;
$riffle = $troop->getElementsByTagName( "shooter" );
$otroop[2] = $riffle->item(0)->nodeValue;
$riffle = $troop->getElementsByTagName( "missle" );
$otroop[3] = $riffle->item(0)->nodeValue;
$tank = $troop->getElementsByTagName( "tank" );
$otroop[4] = $tank->item(0)->nodeValue;
$bomber = $troop->getElementsByTagName( "bomber" );
$otroop[5] = $bomber->item(0)->nodeValue;
$patrol = $troop->getElementsByTagName( "patrol" );
$otroop[6] = $patrol->item(0)->nodeValue;
$surveillance = $troop->getElementsByTagName( "surveillance" );
$otroop[7] = $surveillance->item(0)->nodeValue;
}
XML
<troops>
<sniper level="5">2</sniper>
<riffle level="0">0</riffle>
<shooter level="0">0</shooter>
<missle level="0">0</missle>
<tank level="0">0</tank>
<bomber level="0">0</bomber>
<patrol level="0">0</patrol>
<surveillance level="0">0</surveillance>
</troops>
我想能够拉动每个节点的水平,我也尝试了
$oresearch[0] = $sniper->getAttribute("level");
但这不起作用,这里的任何建议都会有所帮助
答案 0 :(得分:1)
在您的代码中$sniper
是DOMNodeList
。您可以正确处理第一个节点的值:
$sniper->item(0)->nodeValue
对于第一个节点的属性,您必须这样做:
$sniper->item(0)->getAttribute('level')