dongshang3309 2014-12-01 07:21
浏览 29
已采纳

无法使用此大URL生成XML文件

I want to generate XML with this following code but it's giving error as I include big URL. Even cdata is not working:

$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());
  • 写回答

1条回答 默认 最新

  • dongyanggan3025 2014-12-01 09:21
    关注

    You can not add CDATA sections with SimpleXML, only standard text nodes. But even that has a bug. The text can not contain entities (the &...;). The same bug exists in DOMDocument::createElement and DOMNode::$nodeValue. You can create the document with DOM however.

    $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());
    

    You can see that DOM document has a bunch of methods to create the different node types. The nodes have some methods to append the new node to them.

    The methods return the nodes so you can nest them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏