Parsing XML data with Namespaces in PHP

2019-05-21 00:37发布

I'm trying to work with this XML feed that uses namespaces and i'm not able to get past the colon in the tags. Here's how the XML feed looks like:

<r25:events pubdate="2010-05-19T13:58:08-04:00">
<r25:event xl:href="event.xml?event_id=328" id="BRJDMzI4" crc="00000022" status="est">
 <r25:event_id>328</r25:event_id>
 <r25:event_name>Testing 09/2005-08/2006</r25:event_name>
 <r25:alien_uid/>
 <r25:event_priority>0</r25:event_priority>
 <r25:event_type_id xl:href="evtype.xml?type_id=105">105</r25:event_type_id>
 <r25:event_type_name>CABINET</r25:event_type_name>
 <r25:node_type>C</r25:node_type>
 <r25:node_type_name>cabinet</r25:node_type_name>
 <r25:state>1</r25:state>
 <r25:state_name>Tentative</r25:state_name>
 <r25:event_locator>2005-AAAAMQ</r25:event_locator>
 <r25:event_title/>
 <r25:favorite>F</r25:favorite>
 <r25:organization_id/>
 <r25:organization_name/>
 <r25:parent_id/>
 <r25:cabinet_id xl:href="event.xml?event_id=328">328</r25:cabinet_id>
 <r25:cabinet_name>cabinet 09/2005-08/2006</r25:cabinet_name>
 <r25:start_date>2005-09-01</r25:start_date>
 <r25:end_date>2006-08-31</r25:end_date>
 <r25:registration_url/>
 <r25:last_mod_dt>2008-02-27T14:22:43-05:00</r25:last_mod_dt>
 <r25:last_mod_user>abc00296004</r25:last_mod_user>
</r25:event>
</r25:events>

And here is what I'm using for code - I'll trying to throw these into a bunch of arrays where I can format the output however I want:

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://somedomain.com/blah.xml");
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXmlElement($output);
foreach ($xml->events->event as $entry){
  $dc = $entry->children('http://www.collegenet.com/r25');
  echo $entry->event_name . "<br />";
  echo $entry->event_id . "<br /><br />";
}

3条回答
祖国的老花朵
2楼-- · 2019-05-21 01:19

Alternatively, since r25 is the only namespace used and therefore is not especially helpful, I just run

$xml = preg_replace('/r25:/','',$xml);

And that strips out the namespace. Then you can navigate much easier with simplexml, just like in your example.

查看更多
对你真心纯属浪费
3楼-- · 2019-05-21 01:20

"All kinds of errors" isn't a helpful description; what errors are you actually getting?

You should give the object a namespace option like this:

$xml = new SimpleXmlElement($output, null, false, $ns = 'r25');

See the manual.

查看更多
冷血范
4楼-- · 2019-05-21 01:22

Figured out the issue was with the XML feed rather than code:

XML feed was missing this line:

<r25:events xmlns:r25="http://www.collegenet.com/r25" xmlns:xl="http://www.w3.org/1999/xlink" pubdate="2010-05-19T13:58:08-04:00">

Thanks for the help though.

查看更多
登录 后发表回答