简体   繁体   English

在PHP中使用命名空间解析XML数据

[英]Parsing XML data with Namespaces in PHP

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. 我正在尝试使用使用命名空间的XML提要,但无法跨越标记中的冒号。 Here's how the XML feed looks like: XML提要的外观如下:

<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 />";
}

Figured out the issue was with the XML feed rather than code: 找出问题出在XML feed而不是代码:

XML feed was missing this line: XML feed缺少以下行:

<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. 谢谢您的帮助。

"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 . 请参阅手册

Alternatively, since r25 is the only namespace used and therefore is not especially helpful, I just run 另外,由于r25是唯一使用的名称空间,因此并不是特别有用,因此我只运行

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

And that strips out the namespace. 这样就消除了名称空间。 Then you can navigate much easier with simplexml, just like in your example. 然后,就像您的示例一样,您可以使用simplexml轻松导航。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM