开发者

How include and read HTML for a XML?

<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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜