使用PHP在XML中的CDATA中访问标签 [英] Accessing tags inside CDATA in XML using PHP

查看:182
本文介绍了使用PHP在XML中的CDATA中访问标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am confused. How can I access tags inside 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] ) ;
?>

我正在使用它,但是出现黑屏...。

I am using this, but I am getting a blank screen....

推荐答案

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

Your problem is that your font tags are inside of CDATA. Since CDATA stands for "Compiled Data", PHP should treat it as a "block of non-parsed data." It should not (and cannot) let you read those as tags. You'll probably have to do something like:

$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无效,例如< > ,但这似乎是您最好的选择...

Your problem, of course, is that CDATA will let things in which are not valid XML, like < or >, but this seems to be your best option...

这篇关于使用PHP在XML中的CDATA中访问标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆