开发者

Accessing tags inside CDATA in XML using PHP

开发者 https://www.devze.com 2023-03-28 08:55 出处:网络
I am confused. How can I access tags inside CDATA? XML Code: <body> <block> <![CDATA[ <font color=\"#FFCC53\" si开发者_高级运维ze=\"+6\"><b>Latest News Updates</b>&

I am confused. How can I access tags inside CDATA?

XML Code:

<body>
<block>
<![CDATA[ 
     <font color="#FFCC53" si开发者_高级运维ze="+6"><b>Latest News Updates</b></font>
     <font color="#AAAAAA">HTML Formatted Text Fields</font>            
]]>                         
</block>
</body>

PHP Code:

<?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....


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];

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

0

精彩评论

暂无评论...
验证码 换一张
取 消