Jump to content

Specific XML parsing


zackcez

Recommended Posts

Well basically I have an xml file that's set up like this:

<NPCDef-array>
  <NPCDef>
    <name>Unicorn</name>
    <description>It's a unicorn</description>
    <command></command>
    <attack>21</attack>
    <strength>23</strength>
    <hits>19</hits>
    <defense>23</defense>
    <attackable>true</attackable>
    <aggressive>false</aggressive>
    <respawnTime>30</respawnTime>
    <sprites>
      <int>130</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
    </sprites>
    <hairColour>0</hairColour>
    <topColour>0</topColour>
    <bottomColour>0</bottomColour>
    <skinColour>0</skinColour>
    <camera1>201</camera1>
    <camera2>230</camera2>
    <walkModel>6</walkModel>
    <combatModel>6</combatModel>
    <combatSprite>7</combatSprite>
    <drops>
      <ItemDropDef>
        <id>20</id>
        <amount>1</amount>
        <weight>0</weight>
      </ItemDropDef>
      <ItemDropDef>
        <id>466</id>
        <amount>1</amount>
        <weight>0</weight>
      </ItemDropDef>
    </drops>
  </NPCDef>

There's about 10k entries though...I'm wondering if/how it's possible to retrieve something like...say I have a GET variable of name...I'd like it to then look through the file to find the entry with the name in the get...I've looked for quiet a bit and all I've managed to find is how to parse/output all data in  table...

Link to comment
https://forums.phpfreaks.com/topic/138091-specific-xml-parsing/
Share on other sites

Here an example

<?php
$XMLFILE = "<NPCDef-array>
  <NPCDef>
    <name>Unicorn</name>
    <description>It's a unicorn</description>
    <command></command>
    <attack>21</attack>
    <strength>23</strength>
    <hits>19</hits>
    <defense>23</defense>
    <attackable>true</attackable>
    <aggressive>false</aggressive>
    <respawnTime>30</respawnTime>
    <sprites>
      <int>130</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
      <int>-1</int>
    </sprites>
    <hairColour>0</hairColour>
    <topColour>0</topColour>
    <bottomColour>0</bottomColour>
    <skinColour>0</skinColour>
    <camera1>201</camera1>
    <camera2>230</camera2>
    <walkModel>6</walkModel>
    <combatModel>6</combatModel>
    <combatSprite>7</combatSprite>
    <drops>
      <ItemDropDef>
        <id>20</id>
        <amount>1</amount>
        <weight>0</weight>
      </ItemDropDef>
      <ItemDropDef>
        <id>466</id>
        <amount>1</amount>
        <weight>0</weight>
      </ItemDropDef>
    </drops>
  </NPCDef>
  </NPCDef-array>
  ";

?>

<?php
$xml = new SimpleXMLElement($XMLFILE);
echo 'Examples<br><a href="?attr=name">name</a><br><a href="?attr=description">description</a><br><a href="?attr=hits">hits</a><br>';
echo 'Drops<br><a href="?drops=1">1</a><br><a href="?drops=2">2</a><br>';
echo "I got this<br><b>";
if(!empty($_GET['attr'])) echo $xml->{'NPCDef'}->{$_GET['attr']};
if(!empty($_GET['drops']))
{
$i = (int)$_GET['drops']-1;
echo "ID: ".$xml->{'NPCDef'}->{'drops'}->ItemDropDef[$i]->{'id'}."<br>";
echo "amount: ".$xml->{'NPCDef'}->{'drops'}->ItemDropDef[$i]->{'amount'}."<br>";
echo "weight: ".$xml->{'NPCDef'}->{'drops'}->ItemDropDef[$i]->{'weight'}."<br>";
}
echo "</b>";
?>

Link to comment
https://forums.phpfreaks.com/topic/138091-specific-xml-parsing/#findComment-721910
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.