skip to Main Content

I need to parse the “name” and “version” attributes from all of the “<mod>” tag entries.

Thanks to this page I was only able to parse the first “<mod>” tag from the xml.

I’m no programmer, so I have no clue how to go on.

This is my xml file.

These are my testing php files.

$xmlfile = simplexml_load_file("packa.xml");
foreach ($xmlfile->children() as $mods) {
    echo $mods->mod['name']."</br>";
    echo $mods->mod['version'];
}

had this output.

</br></br>Just Enough Items</br>4.15.0.293

And

foreach ($xmlfile->children() as $mods) {
    echo $mods->mod['name']."
    </br>".$mods->mod['version'];
}

had this output

</br>
</br>Just Enough Items
</br>4.15.0.293

2

Answers


  1. You can try something along these lines, using xpath:

    $result = $xmlfile->xpath('//mods//mod');  
    foreach($result as $mod){
        $nam = $mod->xpath('.//@name');
        $ver = $mod->xpath('.//@version');
        echo implode ( $nam). "</br>" .implode( $ver );  
        echo "<br>";   
    }
    

    Output:

    Just Enough Items
    4.15.0.293
    MTLib
    3.0.6
    CraftTweaker2
    1.12-4.1.20
    Mod Tweaker
    4.0.18
    

    etc.

    Login or Signup to reply.
  2. You are using the wrong level in the XML in the foreach() loop, this is only looping over the <mods> elements – and there is only 1. You need to specify that you want to loop over the <mod> elements and then just access the attributes as you already do…

    foreach ($xmlfile->mods->mod as $mod) {
        echo $mod['name'] . "/" . $mod['version'] . "</br>" . PHP_EOL;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search