2

I am building up a script using XML DOM and 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>

I wanna be able to pull the level for each node as well i had tried

$oresearch[0] = $sniper->getAttribute("level");

but that doesn't work, any advice here would be helpful

1 Answer 1

1

In your code $sniper is a DOMNodeList. You handle that correctly where you get the value of the first node:

$sniper->item(0)->nodeValue

For attributes of the first node you have to do it analogous:

$sniper->item(0)->getAttribute('level')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.