我需要编写一个函数来从 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。怎么会?