2

I have the following XML code that I'm trying to parse, but I'm sure of how to traverse some of the data in PHP:

  <entry>
    <id>http://data.treasury.gov:8001/Feed.svc/DailyTreasuryYieldCurveRateData(5360)</id>
    <title type="text"></title>
    <updated>2011-06-09T20:15:18Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(5360)" />
    <category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:Id m:type="Edm.Int32">5360</d:Id>
        <d:NEW_DATE m:type="Edm.DateTime">2011-06-01T00:00:00</d:NEW_DATE>
        <d:BC_1MONTH m:type="Edm.Double">0.04</d:BC_1MONTH>
        <d:BC_3MONTH m:type="Edm.Double">0.05</d:BC_3MONTH>
        <d:BC_6MONTH m:type="Edm.Double">0.11</d:BC_6MONTH>
        <d:BC_1YEAR m:type="Edm.Double">0.18</d:BC_1YEAR>
        <d:BC_2YEAR m:type="Edm.Double">0.44</d:BC_2YEAR>
        <d:BC_3YEAR m:type="Edm.Double">0.74</d:BC_3YEAR>
        <d:BC_5YEAR m:type="Edm.Double">1.6</d:BC_5YEAR>
        <d:BC_7YEAR m:type="Edm.Double">2.28</d:BC_7YEAR>
        <d:BC_10YEAR m:type="Edm.Double">2.96</d:BC_10YEAR>
        <d:BC_20YEAR m:type="Edm.Double">3.83</d:BC_20YEAR>
        <d:BC_30YEAR m:type="Edm.Double">4.15</d:BC_30YEAR>
        <d:BC_30YEARDISPLAY m:type="Edm.Double">4.15</d:BC_30YEARDISPLAY>
      </m:properties>
    </content>
  </entry>

I can only get so far as

entry->content

As the following throws an error for having a colon:

entry->content->m:properties 

How do I access what's inside content such as d:NEW_DATE?

Choy
  • 2,087
  • 10
  • 37
  • 48
  • I am assuming you are using a DOM parser, namespaces (m:) are handled slightly differently. – Devraj Jun 10 '11 at 01:34
  • 1
    Those are namespaced. You have to do something special to access namespaced notes. Are you using simplexml? – Frank Farmer Jun 10 '11 at 01:35
  • See http://stackoverflow.com/questions/1133897/how-do-i-parse-xml-containing-custom-namespaces-using-simplexml – Michael Berkowski Jun 10 '11 at 01:59
  • Ah I see. I am using SimpleXML. After a quick search and looking at the link Michael provided I guess I'm stuck on how to go two levels (m:properties -> d:NEW_DATE) deep... – Choy Jun 10 '11 at 02:00

2 Answers2

6

In SimpleXML you can use the children('prefix', true) and attributes('prefix', true) functions to access namespaced content.

entry->content->children('m', true)->properties

or to access d:NEW_DATE

entry->content->children('m', true)->properties->children('d', true)->NEW_DATE

or one step further to access the m:type attribute

entry->content->children('m', true)->properties->children('d', true)->NEW_DATE->attributes('m', true)->type
fidr
  • 178
  • 1
  • 4
  • Thanks, I figured out how to do it by including the uri of the schema in children(), but was also trying to figure out how to do it without having to include the uri! Do you know if there is a difference in speed the two methods? – Choy Jun 10 '11 at 03:27
0

You can use the SimpleXml's functions SimpleXML

But my fav class is DOMDocument

Jean-Philippe Leclerc
  • 6,713
  • 5
  • 43
  • 66