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.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?