Bri*_*nte 0 php xpath simplexml
我有一个小的XML文件:
<wddxPacket version='1.0'>
<header/>
<data>
<struct type='coldfusion.runtime.ArgumentCollection'>
<var name='HEADLINE'>
<string>Richard Barret's Articles on Leadership and High Performance Organisations</string>
</var>
</struct>
</data>
</wddxPacket>
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用PHP SimpleXML和xpath来提取var名称HEADLINE元素中的字符串元素之间的值.此代码有效:
// Location of the XML file on the file system
$file = 'http://10.10.200.37/skins/importscript/41802.xml';
$xml = simplexml_load_file($file);
// CREATE THE ARRAYS FOR EACH XML ELEMENT NEEDED
$title = $xml->xpath('//var[@name="HEADLINE"]');
echo "<p>";
print_r($title);
echo "</p>";
Run Code Online (Sandbox Code Playgroud)
问题是它不仅返回值,还返回所有数组信息.如:
Array (
[0] => SimpleXMLElement Object (
[@attributes] => Array (
[name] => HEADLINE
)
[string] => Richard Barret's Articles on Leadership and High Performance Organisations
)
)
我怎样才能让它只返回值而没有别的?
如果我替换print_r为echo $title;I,则在页面上输入单词Array而不是值.如果我尝试echo $title[0];我什么也得不到.
我已经尝试了很多东西,现在想不出别的!我究竟做错了什么?有人能指出我正确的方向吗?谢谢!
Bri*_*nte 11
对不起请忽略这个!就在我发布问题之后,我意识到我做错了什么!
对于今天和我一样慢的人来说,问题是......
$title = $xml->xpath('//var[@name="HEADLINE"]');
Run Code Online (Sandbox Code Playgroud)
应该:
$title = $xml->xpath('//var[@name="HEADLINE"]/string');
Run Code Online (Sandbox Code Playgroud)
现在它可以正常工作.
| 归档时间: |
|
| 查看次数: |
1031 次 |
| 最近记录: |