【问题标题】:Accessing tags inside CDATA in XML using PHP使用 PHP 在 XML 中访问 CDATA 中的标签
【发布时间】:2011-08-15 13:52:11
【问题描述】:

我很困惑。如何访问 CDATA 中的标签?

XML 代码:

<body>
<block>
<![CDATA[ 
     <font color="#FFCC53" size="+6"><b>Latest News Updates</b></font>
     <font color="#AAAAAA">HTML Formatted Text Fields</font>            
]]>                         
</block>
</body>

PHP 代码:

<?php
     $xml = simplexml_load_file("main.xml");
     print (  $xml->smallTextList[0]->item[0]->textBody[0]->font[0] ) ;
?>

我正在使用这个,但我得到一个空白屏幕....

【问题讨论】:

标签: php xml cdata


【解决方案1】:

您的问题是您的字体标签在CDATA 的内部。由于 CDATA 代表“编译数据”,PHP 应该将其视为“未解析数据块”。它不应该(也不能)让您将它们作为标签阅读。您可能必须执行以下操作:

$xml = simplexml_load_file("main.xml");
$inner = simplexml_load_string( 
 '<fk>' . // you have to wrap the CDATA in a tag, otherwise it will break.
      // not sure about asXML. You may be able to get away without it.
      $xml->block[0]->asXML() . 
 '</fk>'
 );
print $inner->font[0];

当然,您的问题是 CDATA 会允许包含无效 XML 的内容,例如 &lt;&gt;,但这似乎是您的最佳选择...

【讨论】:

  • 我有它并收到此错误消息。致命错误:在线调用 C:\xampp\htdocs\xml\forum\1.php 中非对象上的成员函数 asXML() 7
  • @junaid 这或多或少是从您上面的代码中复制而来的,这意味着您的smallTextList[0]-&gt;item 不存在。
猜你喜欢
  • 1970-01-01
  • 2013-07-03
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 2020-11-27
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多