I need to take XML DATA and upload all of it into my MySQL database. (right-click, save as...)
Here's my theory...
Take this line for example:
monster short="Fredzin the crazed Dwarf" info="aggr, kungfu, cancellation" race="Dwarf" eq="A cheaply made grey shirt, A pressed pair of black trousers" exp="120280" palpoints="93" align="evil" god="Teros"
I want to find the index of "short=" and then find the index of the quote marks following the word Dwarf so that "Fredzin the crazed Dwarf" is the value that will be uploaded. But I can't figure out how to find the index of the 2nd quote mark, and then capture everything in between.
I need to do this for ALL XML attribute values. Keep in mind that not all of the XML has the same attributes. So I don't think I can do something like - gather all of the "short" values, upload to db, then go through and gather the "exp" values, etc. I need to do one line at a time to ensure that the XML data is uploaded to the proper record in the db.
Any suggestions? I've tried a few variations of substr(), simpleXML, explode() and fopen() but to no avail... yet.
Note: I've made some minor adjustments to your XML so it would be well-formed:
<areas>
node;<area>
node.From your question tag I assume you're trying to use SimpleXML, so here's a code snippet that can help you retrieve the data you want.
<?php
$xml = <<<XML
<areas>
<area name="Abjurer Grove">
<monster short="Nikwasli the Catfolk Apprentice" exp="319112" info="acid arrow" race="Catfolk" align="good" god="none" />
<monster short="An extra fluffy sheep" exp="49271" race="Sheep" align="good" god="none" />
<monster short="Slireistar the Treant Master Abjurer" race="Treant" align="good" god="Silvain" />
<monster short="Alkinoyine the Djinni Master Abjurer" race="Djinni" align="good" god="Aquaris" />
<monster short="Arthorian the Guildmaster of the Abjurers" align="good" god="none" race="Human" />
<monster short="Katalare the Ent Master Abjurer" race="Ent" align="good" god="Silvain" />
<monster short="An older dwarf with a long beard and a sharp pick" race="Dwarf" eq="A well used iron pick (2h), A pair of brown leggings" info="kick, brawl?" exp="271681" />
<monster short="A older dwarf named Hargris Doc, leaning on his pick" race="Dwarf" eq="A light blue working-class shirt, A well used iron pick (2h), A simple cloth belt, A warm pair of woolen pants, An expertly made pair of high quality boots" align="sli good" god="none" />
<monster short="A grumpy young dwarf is mining here" race="Dwarf" eq="A well used iron pick (2h), An olive coloured pair of pants / A pair of shiney black leather pants with silver studs" exp="256087" />
<monster short="Fredzin the crazed Dwarf" info="aggr, kungfu, cancellation" race="Dwarf" eq="A cheaply made grey shirt, A pressed pair of black trousers" exp="120280" palpoints="93" align="evil" god="Teros" />
<monster short="Zoreil the Mind flayer Adept Apprentice" race="Mind flayer" exp="1151225" align="evil" god="Kirvana" info="tentacle drain (drains sps), acid arrow" />
<monster short="Anthiala, a beautiful elven scholar working in the tower" race="Elf" />
<monster short="A hard working grumpy dwarf swinging a pick" exp="245112" info="kick" race="Dwarf" eq="A well used iron pick (2h), A pair of brown leggings" align="evil" palpoints="126" god="Teros" />
<monster short="A dwarf, covered in grime from a day in the mines" exp="228719" align="evil" palpoints="111" eq="A well used iron pick (2h), A plain white pair of pants" />
</area>
<area name="Agnarock">
<monster short="The Icedragon 'Kel'ba'rash'" exp="5755726" race="Dragon" palpoints="3000" align="extr evil" god="none" />
<monster short="Muscular Giant Recruit." exp="138754" eq="Giants breastplate" race="Giant" align="good" god="Silvain" />
<monster short="Strong looking outer valley Giant" exp="141396" eq="Giants breastplate" race="Giant" align="good" god="Silvain" />
<monster short="A strong looking giant recruit" exp="67153" race="Giant" eq="Thick wooden shield, Ugly Giant boots (smelling)" align="good" god="Silvain" />
<monster short="A hairy, strong looking elite Giant warrior" exp="294573" race="Giant" info="bladed fury" eq="A Lochaber (2h), A huge giant sword (2h), Giant leather pants" align="good" god="Silvain" />
<monster short="Blue wearing Giant warrior" exp="463955" eq="Battlemail of the Giant Army, A shimmering Halberd (2h), Giant leather pants" race="Giant" align="sli good" god="Silvain" />
<monster short="All black Giant Fighter" exp="1271226" race="Giant" align="sli good" god="Silvain" eq="A multicolored cloak, Elite Platemail of Angarock, A shimmering Halberd (2h) x 2, Giant leather pants" />
<monster short="A female giant warrior of Angarock" exp="3135812" race="Giant" />
<monster short="Karbitsch the Hobbit, Herald of Shakadoom." exp="6559714" race="Hobbit" eq="Karbitsch's pants of Quickness, Speed shoes of Karbitsch" />
<monster short="Shaikuub, Grandwarriorlord of Angarock" exp="5100480" race="Giant" eq="Black axe named 'Deathstorm' (2h), The Executioner (2h)" />
<monster short="Deschoga the Djinni, Unholy Master of the Assassins." exp="10759793" race="Djinni" eq="The Bonemask of Disappearence, Gaseous Gloves, Claws of Assassination (2h)" />
</area>
</areas>
XML;
// Retrieve each monster node's short attribute using XPath
$sxe = new SimpleXMLElement($xml);
$monsters = $sxe->xpath('//monster');
// Iterate all monster nodes
foreach ($monsters as $monster) {
// Retrieve each monster node attributes
$monsterAttributes = $monster->attributes();
print_r($monsterAttributes);
// Database insert operation
}
I didn't include the whole output, you can check that in this codepad live example. Notice how each of these objects reflect the different attributes from each node. All you have to do is process them as store them at your will in your DB.
SimpleXMLElement Object
(
[@attributes] => Array
(
[short] => Nikwasli the Catfolk Apprentice
[exp] => 319112
[info] => acid arrow
[race] => Catfolk
[align] => good
[god] => none
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[short] => An extra fluffy sheep
[exp] => 49271
[race] => Sheep
[align] => good
[god] => none
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[short] => Slireistar the Treant Master Abjurer
[race] => Treant
[align] => good
[god] => Silvain
)
)
In this simple case you may use regex for parse needed part. In PHP it will be look like that:
<?php
$content = file_get_contents('monsters.xml');
$matches = array();
$pattern = "/short=\"([^\"]+)\"/";
preg_match_all ($pattern , $content, $matches);
if (isset($matches[1])) {
foreach ($matches[1] as $monster) {
echo $monster. "
";
}
}
Result for monster.xml:
Nikwasli the Catfolk Apprentice
An extra fluffy sheep
Slireistar the Treant Master Abjurer
Alkinoyine the Djinni Master Abjurer
Arthorian the Guildmaster of the Abjurers
Katalare the Ent Master Abjurer
An older dwarf with a long beard and a sharp pick
....