我正在使用 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");
但这不起作用,这里的任何建议都会有所帮助