0

Ive been trying every way possible to create cdata entries in my xml. My latest attempt is as follows. I can't even get passed for the first statement where im creating a new DOMDocument. Any ideas?

<?php
$xml = '
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
    <make name="Ford">
        <model>Mustang</model>
    </make>
    <make name="Honda">
        <model>Accord</model>
    </make>
</cars>
';

$dom = new DOMDocument;

$dom->loadXML($xml);


$xml = simplexml_import_dom($dom);
print "working";
?>

2 Answers 2

1

You should not have any characters before the XML declaration. Remove the line break at $xml = '.

The neatest solution would be to use heredoc syntax:

$xml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
    <make name="Ford">
        <model>Mustang</model>
    </make>
    <make name="Honda">
        <model>Accord</model>
    </make>
</cars>
XML;
Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at: DOMDocument::createCDATASection

$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
    <make name="Ford">
        <model>Mustang</model>
    </make>
    <make name="Honda">
        <model>Accord</model>
    </make>
</cars>
';

$dom = new DOMDocument;
$dom->loadXML($xml);

$cdataNode = $dom->createCDATASection('<&>');
$dom->documentElement->appendChild($cdataNode);

echo $dom->saveXml();

Output:

<?xml version="1.0" encoding="ISO-8859-1"?>
<cars>
    <make name="Ford">
        <model>Mustang</model>
    </make>
    <make name="Honda">
        <model>Accord</model>
    </make>
<![CDATA[<&>]]></cars>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.