开发者

How include and read HTML for a XML?

开发者 https://www.devze.com 2023-04-01 01:16 出处:网络
<myxml> <bla> I want <strong>HTML here</strong> </bla&g开发者_JAVA技巧t;
<myxml>
  <bla>
    I want <strong>HTML here</strong>
  </bla&g开发者_JAVA技巧t;
</myxml>

How can I read the HTML from the XML document?

$data = file_get_contents('myxml.xml');
$xml = new SimpleXMLElement($data);
print_r($xml); // fail...

ps: without escaping, because it's annoying to escape the text each time I add something..


edit:

<myxml>
  <bla><![CDATA[I want <strong>HTML here</strong>]]></bla>
</myxml>

the PHP:

$xml = simplexml_load_file('myxml.xml');
print_r($xml);

and the output is:

SimpleXMLElement Object
(
    [bla] => SimpleXMLElement Object
        (
        )

)

no cdata there..


Surround your I want <strong>HTML here</strong> with CDATA tags, as such:

<![CDATA[
    I want <strong>HTML here</strong>
]]>

This will tell parsers to ignore what's inside the CDATA block and just parse it as plaintext.


Explicit typecast is not required for text nodes, and it works only there; print_r() is the wrong function/language feature to use here (use echo instead).

For printing the content of any SimpleXML element as XML (one that may also contain other elements), use its asXML() method.

0

精彩评论

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