Search code examples
phphtmlxmlsimplexmlcdata

How include and read HTML for a XML?


<myxml>
  <bla>
    I want <strong>HTML here</strong>
  </bla>
</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..


Solution

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