1

我需要获取 XML DATA 并将其全部上传到我的 MySQL 数据库中。(右键,另存为...)

这是我的理论...

以这一行为例:

monster short="Fredzin the crazed Dwarf" info="aggr, kungfu, cancel" race="Dwarf" eq="一件廉价的灰色衬衫,一条压过的黑色裤子" exp="120280" palpoints="93" align ="邪恶" 神="特罗斯"

我想找到 "short=" 的索引,然后找到 Dwarf 后面的引号的索引,以便 "Fredzin the crazed Dwarf" 是要上传的值。但我不知道如何找到第二个引号的索引,然后捕获两者之间的所有内容。

我需要为所有 XML 属性值执行此操作。请记住,并非所有 XML 都具有相同的属性。所以我认为我不能做类似的事情 - 收集所有“短”值,上传到数据库,然后通过并收集“exp”值等。我需要一次做一行以确保XML 数据上传到数据库中的正确记录。

有什么建议么?我尝试了 substr()、simpleXML、explode() 和 fopen() 的一些变体,但无济于事……但。

4

2 回答 2

1

注意:我对你的 XML 做了一些小的调整,所以它的格式是正确的:

  1. 添加了一个顶级<areas>节点;
  2. <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
        )
)
于 2013-05-07T19:32:07.320 回答
1

在这种简单的情况下,您可以使用正则表达式来解析所需的部分。在 PHP 中,它看起来像这样:

<?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. "\n";
    }
}

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
....
于 2013-05-07T19:20:07.923 回答