我想使用以下代码生成XML,但是因为我包含大URL而导致错误。甚至cdata都不起作用:
$xml = new SimpleXMLElement('<xml/>');
for ($i = 1; $i <= 8; ++$i) {
$track = $xml->addChild('track');
$track->addChild('path', "data[");
$track->addChild('title', "<![CDATA[http://r8---sn-5hn7sn7k.googlevideo.com/videoplayback?mt=1417417897&mv=m&ms=au&ip=2001:1af8:4700:a022:1::4ae9&itag=18&initcwndbps=4948750&mm=31&sver=3&id=o-AB1_DFOem6qVMtWki7uWj0CcIevyqEaY_OtwcbRPKZXT&ipbits=0&upn=IFgTkloUxQQ&expire=1417439567&fexp=902522,907259,922247,927622,932404,935694,941004,942810,943909,947209,948124,952302,952605,952901,953912,957103,957105,957201&key=yt5&sparams=dur,id,initcwndbps,ip,ipbits,itag,mm,ms,mv,source,upn,expire&source=youtube&signature=7266EE8B52AAB9E3C6DECBADDD112BDF00E85EFB.362737C47ECAE4D9E25E900E8C24483799B2A8F8&dur=1554.831&title=8+Stunning+Linguistic+Miracles+of+The+Holy+Quran+%7C+Kinetic+Typography]]>");
}
Header('Content-type: text/xml');
print($xml->asXML());
答案 0 :(得分:0)
您不能使用SimpleXML添加CDATA部分,只能添加标准文本节点。但即便如此,也有一个错误。文本不能包含实体(&...;
)。 DOMDocument::createElement
和DOMNode::$nodeValue
中存在相同的错误。但是,您可以使用DOM创建文档。
$dom = new DOMDocument();
$root = $dom->appendChild($dom->createElement('xml'));
for ($i = 1; $i <= 8; ++$i) {
$track = $root->appendChild($dom->createElement('track'));
$track
->appendChild($dom->createElement('path'))
->appendChild($dom->createTextNode('data['));
$track
->appendChild($dom->createElement('title'))
->appendChild($dom->createCDATASection("http://r8---sn-5hn7sn7k.googlevideo.com/videoplayback?mt=1417417897&mv=m&ms=au&ip=2001:1af8:4700:a022:1::4ae9&itag=18&initcwndbps=4948750&mm=31&sver=3&id=o-AB1_DFOem6qVMtWki7uWj0CcIevyqEaY_OtwcbRPKZXT&ipbits=0&upn=IFgTkloUxQQ&expire=1417439567&fexp=902522,907259,922247,927622,932404,935694,941004,942810,943909,947209,948124,952302,952605,952901,953912,957103,957105,957201&key=yt5&sparams=dur,id,initcwndbps,ip,ipbits,itag,mm,ms,mv,source,upn,expire&source=youtube&signature=7266EE8B52AAB9E3C6DECBADDD112BDF00E85EFB.362737C47ECAE4D9E25E900E8C24483799B2A8F8&dur=1554.831&title=8+Stunning+Linguistic+Miracles+of+The+Holy+Quran+%7C+Kinetic+Typography"));
}
$dom->formatOutput = TRUE;
print($dom->saveXML());
您可以看到DOM文档有许多方法来创建不同的节点类型。节点有一些方法可以将新节点附加到它们。
方法返回节点,以便您可以嵌套它们。