关于以下XML。如何使用对象运算符( - >)?
引用<m:properties>
的子代
$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();
没有。我正在读here,在名称空间前缀和元素名称/属性名称之间放置一个冒号(“:”),因此properties
是元素名称,因此不确定为什么不这样做。此问题特别关注使用对象运算符->
和children()
函数。我想知道为什么XML doc的不同级别的相同逻辑表现不同;这与诸如this之类的问题不同,这些问题正在寻找解析XML数据的任何解决方案,无论它是否使用对象运算符->
和children()
函数。
<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>
答案 0 :(得分:1)
您可以使用xpath
的解决方案,并在查询中指定命名空间:
$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
}