开发者

SimpleXML PHP - Why DOM functions don't work, cdata trouble

开发者 https://www.devze.com 2023-03-24 02:41 出处:网络
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?

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";
?>


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;


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

精彩评论

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