编写一个函数来解析XML文件中的数据

时间:2013-07-10 15:40:13

标签: php xml parsing xpath simplexml

我需要编写一个函数来从XML文件中获取数据。

这是我第一次尝试做这样的事情。我想像对待一个mysql数据库那样对待它(是的,我是新手)。

XML的结构如下:

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<PlayerCount value="2">
    <Position value="Left">
        <Action value="Hit">
            <Range BBIdx="2" model="Math">0.653</Range>
            <Range BBIdx="3" model="Math">0.341</Range>
            <Range BBIdx="4" model="Math">0.216</Range>
        </Action>
        <Action value="Punch">
            <Range BBIdx="0" model="Math">1.000</Range>
            <Range BBIdx="1" model="Math">1.000</Range>
            <Range BBIdx="2" model="Math">0.498</Range>
            <Range BBIdx="3" model="Math">0.256</Range>
            <Range BBIdx="4" model="Math">0.168</Range>
        </Action>
        <Action value="Defend">
            <Range BBIdx="1" model="Math">1.000</Range>
            <Range BBIdx="2" model="Math">1.000</Range>
            <Range BBIdx="3" model="Math">0.503</Range>
            <Range BBIdx="4" model="Math">0.341</Range>
        </Action>
    </Position>
    <Position value="Right">
        <Action value="Hit">
            <Range BBIdx="2" model="Math">0.828</Range>
            <Range BBIdx="3" model="Math">0.416</Range>
            <Range BBIdx="4" model="Math">0.278</Range>
        </Action>
        <Action value="Punch">
            <Range BBIdx="1" model="Math">1.000</Range>
            <Range BBIdx="2" model="Math">0.623</Range>
            <Range BBIdx="3" model="Math">0.309</Range>
            <Range BBIdx="4" model="Math">0.217</Range>
        </Action>
        <Action value="HitDefend">
            <Range BBIdx="2" model="Math">0.628</Range>
            <Range BBIdx="3" model="Math">0.313</Range>
            <Range BBIdx="4" model="Math">0.204</Range>
        </Action>
        <Action value="PunchDefend">
            <Range BBIdx="1" model="Math">0.755</Range>
            <Range BBIdx="2" model="Math">0.380</Range>
            <Range BBIdx="3" model="Math">0.192</Range>
            <Range BBIdx="4" model="Math">0.127</Range>
        </Action>
    </Position>
</PlayerCount>
<PlayerCount value="3">
    <Position value="Up">
        <Action value="Hit">
            <Range BBIdx="2" model="Math">0.455</Range>
            <Range BBIdx="3" model="Math">0.237</Range>
            <Range BBIdx="4" model="Math">0.158</Range>
        </Action>
        <Action value="Punch">
            <Range BBIdx="0" model="Math">0.931</Range>
            <Range BBIdx="1" model="Math">0.702</Range>
            <Range BBIdx="2" model="Math">0.372</Range>
            <Range BBIdx="3" model="Math">0.178</Range>
            <Range BBIdx="4" model="Math">0.117</Range>
        </Action>
        <Action value="Defend">
            <Range BBIdx="1" model="Math">1.000</Range>
            <Range BBIdx="2" model="Math">0.725</Range>
            <Range BBIdx="3" model="Math">0.352</Range>
            <Range BBIdx="4" model="Math">0.235</Range>
        </Action>
    </Position>
    <Position value="Left">
        <Action value="Hit">
            <Range BBIdx="2" model="Math">0.576</Range>
            <Range BBIdx="3" model="Math">0.273</Range>
            <Range BBIdx="4" model="Math">0.189</Range>
        </Action>
        <Action value="Punch">
            <Range BBIdx="0" model="Math">1.000</Range>
            <Range BBIdx="1" model="Math">0.862</Range>
            <Range BBIdx="2" model="Math">0.434</Range>
            <Range BBIdx="3" model="Math">0.209</Range>
            <Range BBIdx="4" model="Math">0.150</Range>
        </Action>
        <Action value="Defend">
            <Range BBIdx="1" model="Math">1.000</Range>
            <Range BBIdx="2" model="Math">0.844</Range>
            <Range BBIdx="3" model="Math">0.439</Range>
            <Range BBIdx="4" model="Math">0.291</Range>
        </Action>
        <Action value="HitDefend">
            <Range BBIdx="2" model="Math">0.429</Range>
            <Range BBIdx="3" model="Math">0.212</Range>
            <Range BBIdx="4" model="Math">0.142</Range>
        </Action>
        <Action value="PunchDefend">
            <Range BBIdx="0" model="Math">0.693</Range>
            <Range BBIdx="1" model="Math">0.530</Range>
            <Range BBIdx="2" model="Math">0.269</Range>
            <Range BBIdx="3" model="Math">0.133</Range>
...
<PlayerCount value="3">
...

我希望能够轻松访问<Range>标记之间的数字。

像获取数字

之类的东西
<PlayerCount value="2">
<Position value="Left">
<Action value="Hit">
<Range BBIdx="2" model="Math">

如果是mysql我会做类似的事情:

$query = "SELECT range FROM modeling WHERE BB='' AND POS='' AND OPP='";

任何帮助都非常感谢。我浪费了2天看SimpleXML PHP类,但我不知道如何搜索属性,就像在这种情况下一样。我相信你解决这个问题的时间不会超过2分钟。

如果我有像getnumber这样的函数('file-xml','playercount','position','action','bbidx'),那将是完美的;

提前致谢。

这是我写的代码,但我不知道如何使用属性:

$xml = new SimpleXMLElement($input);

$res = $xml->xpath('/Playercount[.=2]/Position[.=Left]/Action[.=Hit]/Range[.=2]');

while(list( , $node) = each($result)) {
    echo 'result: ',$node,"\n";
}

不幸的是,这不能按预期工作。

这是我现在正在尝试使用的代码:

 <?php


$x = <<<XML
<?xml version='1.0' standalone='yes'?>
<document>
<PlayerCount value="2">
    <Position value="Left">
        <Action value="Hit">
            <Range BBIdx="2" model="Math">0.653</Range>
            <Range BBIdx="3" model="Math">0.341</Range>
            <Range BBIdx="4" model="Math">0.216</Range>
        </Action>
        <Action value="Punch">
            <Range BBIdx="0" model="Math">1.000</Range>
            <Range BBIdx="1" model="Math">1.000</Range>
            <Range BBIdx="2" model="Math">0.498</Range>
            <Range BBIdx="3" model="Math">0.256</Range>
            <Range BBIdx="4" model="Math">0.168</Range>
        </Action>
        <Action value="Defend">
            <Range BBIdx="1" model="Math">1.000</Range>
            <Range BBIdx="2" model="Math">1.000</Range>
            <Range BBIdx="3" model="Math">0.503</Range>
            <Range BBIdx="4" model="Math">0.341</Range>
        </Action>
    </Position>
    <Position value="Right">
        <Action value="Hit">
            <Range BBIdx="2" model="Math">0.828</Range>
            <Range BBIdx="3" model="Math">0.416</Range>
            <Range BBIdx="4" model="Math">0.278</Range>
        </Action>
        <Action value="Punch">
            <Range BBIdx="1" model="Math">1.000</Range>
            <Range BBIdx="2" model="Math">0.623</Range>
            <Range BBIdx="3" model="Math">0.309</Range>
            <Range BBIdx="4" model="Math">0.217</Range>
        </Action>
        <Action value="HitDefend">
            <Range BBIdx="2" model="Math">0.628</Range>
            <Range BBIdx="3" model="Math">0.313</Range>
            <Range BBIdx="4" model="Math">0.204</Range>
        </Action>
        <Action value="PunchDefend">
            <Range BBIdx="1" model="Math">0.755</Range>
            <Range BBIdx="2" model="Math">0.380</Range>
            <Range BBIdx="3" model="Math">0.192</Range>
            <Range BBIdx="4" model="Math">0.127</Range>
        </Action>
    </Position>
</PlayerCount>
</document>
XML;

$xml = simplexml_load_string($x);

//$result = (string)$xml->xpath("//PlayerCount[@value='2']/Position[@value='Left']/Action[@value='Hit']/Range[@BBIdx='2' and @model='Math']")[0];

list($result) = (string)$xml->xpath("//document/PlayerCount[@value='2']/Position[@value='Left']/Action[@value='Hit']/Range[@BBIdx='2' and @model='Math']");
var_dump($result);

?>

Var_dump返回NULL。怎么样?

1 个答案:

答案 0 :(得分:1)

您的xpath几乎就在那里,但使用@来表示属性:

$result = (string)$xml->xpath("//PlayerCount[@value='2']/Position[@value='Left']/Action[@value='Hit']/Range[@BBIdx='2' and @model='Math']")[0];

这适用于PHP&gt; = 5.4并返回第一个选定值 看它工作:http://codepad.viper-7.com/fI1Dh6

使用PHP&lt; 5.4:

list($result) = $xml->xpath(...);
$result = (string)$result;  

或做......

$result = $xml->xpath("//PlayerCount[@value='2']/Position[@value='Left']/Action[@value='Hit']/Range[@BBIdx='2' and @model='Math']");
$result = (string)$result[0];

看到它有效:http://codepad.viper-7.com/2DhUiN