注意:我对你的 XML 做了一些小的调整,所以它的格式是正确的:
- 添加了一个顶级
<areas>
节点;
<area>
为每个节点添加了相应的关闭标签。
从您的问题标签中,我假设您正在尝试使用SimpleXML,所以这里有一个代码片段可以帮助您检索所需的数据。
<?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
}
输出:
我没有包含整个输出,您可以在此键盘实时示例中查看。注意这些对象中的每一个如何反映每个节点的不同属性。您所要做的就是将它们处理为按照您的意愿将它们存储在您的数据库中。
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
)
)