简体   繁体   English

使用属性作为选择器的XML数据

[英]XML Data using attribute as selector

How can I return the '3' in: 如何在以下位置返回“ 3”:

<element type="forecast_icon_code">3</element>

from this external xml: 从这个外部xml:

ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10450.xml ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10450.xml

Here is my function: 这是我的功能:

function state($Sitelink) {

$url = $Sitelink;

$file = $url;
if(!$xml = simplexml_load_file($file))
exit('Failed to open '.$file);

$result = $xml->forecast->forcast-period->element;
print_r($result);
}

Calling the function using: 使用以下命令调用该函数:

state("ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10450.xml")

You need some xpath action. 您需要一些xpath操作。

How extract XML data using PHP 如何使用PHP提取XML数据

Has an example using the same XML data that should give you a start. 有一个使用相同XML数据的示例,应该为您入门。

I found that SimpleXMLElement has some trouble parsing elements with hyphens in the middle like you have above in "forcast-period". 我发现SimpleXMLElement在解析中间带有连字符的元素时遇到了一些麻烦,就像您在“ forcast-period”中所遇到的一样。

I found myself in a similar situation and solved it using xpath. 我发现自己处于类似情况,并使用xpath解决了该问题。

Refer to the following resource and check usage for SimpleXMLElement::xpath(): 请参考以下资源,并检查SimpleXMLElement :: xpath()的用法:

SimpleXMLElement xpath doc SimpleXMLElement xpath文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM