【问题标题】:Parsing XML data with Namespaces in PHP在 PHP 中使用命名空间解析 XML 数据
【发布时间】:2010-05-19 19:21:07
【问题描述】:

我正在尝试使用这个使用命名空间的 XML 提要,但我无法通过标签中的冒号。 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>

这是我用于代码的内容 - 我将尝试将它们放入一堆数组中,在那里我可以根据需要格式化输出:

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

【问题讨论】:

    标签: php xml parsing namespaces simplexml


    【解决方案1】:

    发现问题出在 XML 提要而不是代码上:

    XML 提要缺少这一行:

    <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">
    

    感谢您的帮助。

    【讨论】:

      【解决方案2】:

      “各种错误”不是一个有用的描述;您实际上遇到了什么错误?

      你应该像这样给对象一个命名空间选项:

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

      the manual

      【讨论】:

      • 你的命名空间选项给出了这个错误:致命错误:未捕获的异常 'Exception' 带有消息 'SimpleXMLElement::__construct() 最多需要 3 个参数,给定 4 个参数'
      • 您使用的是旧版本的 PHP 吗?
      • 我遇到了更多类似这样的错误: 警告:SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: namespace error : Namespace prefix r25 on events is not defined in /home/test/demo/other.php 第 59 行警告:SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: 在 /home/test/demo/other.php 第 59 行
      【解决方案3】:

      另外,由于 r25 是唯一使用的命名空间,因此不是特别有用,我只是运行

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

      这样就去掉了命名空间。然后,您可以使用 simplexml 更轻松地导航,就像在您的示例中一样。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-11
        • 1970-01-01
        • 2013-04-30
        • 1970-01-01
        • 2021-04-06
        • 2019-03-24
        • 2021-05-03
        相关资源
        最近更新 更多