douxue7196 2013-05-07 19:00
浏览 47
已采纳

使用PHP转换XML数据

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.

  • 写回答

2条回答 默认 最新

  • dongqiongzheng0615 2013-05-07 19:32
    关注

    Note: I've made some minor adjustments to your XML so it would be well-formed:

    1. Added a top level <areas> node;
    2. Added the corresponding close tag for each <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
    }
    

    Output:

    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
            )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?