I am trying to get a group of attributes from an XML file based on the ID. I have looked around and haven't found a solution that works.
Here is my XML:
<data>
<vico>2</vico>
<vis>
<vi>
<id>1</id>
<name>Vill1</name>
<att>2</att>
<hp>100</hp>
<xp>10</xp>
</vi>
<vi>
<id>2</id>
<name>Vill2</name>
<att>3</att>
<hp>120</hp>
<xp>12</xp>
</vi>
</vis>
</data>
What I am looking to do is create a script that takes the value of vico and does mt_rand(0,vico) (Already created this part of the code), and based on the number that comes up, it pulls that node.
For instance, if the number is 2, I would like it to pull all of the attributes where the id in the xml file is 2. I am thinking I have to make the ID a parent of the other attributes, but I am not sure. For the life of me I can't figure this out. I also want to make sure this is even possible before I invest anymore time.
I do realize this can be done very easily with mySQL but I have chose not to do it that way for portability as I am working off a thumb drive. Any help would be greatly appreciated.
Working code for pulling based on ID:
$xml = simplexml_load_file("vi.xml")
or die("Error: Cannot create object");
$vc = $xml->vico;
$select = mt_rand()&$vc;
if ($select == 0) {
$select = $select + 1;
}
$result = $xml->xpath("/data/vis/vi/id[.='".$select."']/parent::*");
if ($result) {
$node = $result[0];
$name = $node->name;
$att = $node->att;
$hp = $node->hp;
$xp = $node->xp ;
} else {
// nothing found for this id
}