[英]XML Data using attribute as selector
<element type="forecast_icon_code">3</element>
ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10450.xml ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10450.xml
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);
}
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()的用法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.