如何使用对象运算符引用带有名称空间前缀的XML元素? [英] How do I reference an XML element with a namespace prefix using the object operator?

查看:71
本文介绍了如何使用对象运算符引用带有名称空间前缀的XML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于以下XML.如何使用对象运算符(->)引用<m:properties>的子代?

With regard to the below XML. How do I reference the children of <m:properties> using the object operator (->)?

$url = "http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData?$filter=month(NEW_DATE)%20eq%2011%20and%20year(NEW_DATE)%20eq%202015";
$xml = simplexml_load_file($url);
foreach( $xml->entry as $entry ) {
 $element = $xml->entry->content->properties->children();
}

$xml->entry->children();有效,但$xml->entry->content->properties->children();无效.我正在此处读入一个冒号(:")在名称空间前缀和元素名称/属性名称之间,因此properties因此是元素名称,所以不确定为什么不这样.这个问题专门与使用对象运算符->children()函数有关.我想知道为什么在XML文档的不同级别上相同的逻辑表现不同?这与诸如 this 之类的问题有所不同.解析XML数据的解决方案,无论是否使用对象运算符->children()函数.

$xml->entry->children(); works but $xml->entry->content->properties->children(); does not. I'm reading here that a colon (":") is placed between the namespace prefix and the element name/ attribute name so properties is therefore the element name, so not sure why not. This question is specifically concerned with using the object operator -> and children() function. I want to know why the same logic at different levels of the XML doc behaves differently; this differs to questions such as this that are looking for any solution to parsing XML data whether it be using the object operator -> and children() function or not.

<entry xmlns="http://www.w3.org/2005/Atom">
<id>
http://data.treasury.gov:8001/Feed.svc/DailyTreasuryYieldCurveRateData(6257)
</id>
<title type="text"/>
<updated>2015-11-15T13:40:16Z</updated>
<author>
<name/>
</author>
<link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(6257)"/>
<category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<content type="application/xml">
<m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<d:Id xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Int32">6257</d:Id>
<d:NEW_DATE xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.DateTime">2015-01-02T00:00:00</d:NEW_DATE>
<d:BC_1MONTH xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">0.02</d:BC_1MONTH>
<d:BC_3MONTH xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">0.02</d:BC_3MONTH>
<d:BC_6MONTH xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">0.11</d:BC_6MONTH>
<d:BC_1YEAR xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">0.25</d:BC_1YEAR>
<d:BC_2YEAR xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">0.66</d:BC_2YEAR>
<d:BC_3YEAR xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">1.07</d:BC_3YEAR>
<d:BC_5YEAR xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" m:type="Edm.Double">1.61</d:BC_5YEAR>


</m:properties>
</content>
</entry>

推荐答案

您可以将解决方案与xpath一起使用,并在查询中指定名称空间:

You could use a solution with xpath and specify the namespace in the query:

$url = "http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData?$filter=month(NEW_DATE)%20eq%2011%20and%20year(NEW_DATE)%20eq%202015";
$xml = simplexml_load_file($url);

foreach ($xml->entry as $entry) { // loop over the entries
    print_r($entry->xpath('//d:BC_3MONTH')); // gives you the actual BC_3MONTH
    print_r($entry->xpath('//d:Id')); // the actual ID
}

这篇关于如何使用对象运算符引用带有名称空间前缀的XML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆