douan4106 2012-07-06 16:36
浏览 31

使用SimpleXML和WITHOUT dom_import_simplexml()将CDATA添加到XML元素

I have a deadline for this project (Monday). It worked great on the localhost, but when I uploaded it to our web server, I discovered that we do not have all of the DOM package enabled and I cannot use the function dom_import_simplexml(). My server admin is ignoring my requests, probably because of the short notice, and I cannot possibly rewrite the XML system into a database system that quickly.

This appears to be the only error I'm encountering. Please, if you have any alternative ideas, I'd love to hear them. I'm at a loss, because I can't find any other solution. All I have to do is create some XML elements and populate them with CDATA values, and I can't believe that this is not supported by SimpleXML!

Please, are there any alternatives you can think of? I'm open, because I don't know what I can do.

  // Add a <pages /> element
  $xml->addChild('pages');
  // Populate it with data 
  foreach($pages as $page){
   $new = $xml->pages->addChild('page');
   $new->addAttribute('pid', $page['pid']);
   $new->addAttribute('title', $page['title']);
   if(isset($page['ancestor']))
    $new->addAttribute('ancestor', $page['ancestor']);
   $node = dom_import_simplexml($new);
   $no = $node->ownerDocument;
   $node->appendChild($no->createCDATASection($page['content']));
  }

Those last three lines obviously won't work, and I don't know what else I can do with them!

Thank you so much for any help you can provide.

  • 写回答

1条回答 默认 最新

  • doumu6941 2014-07-17 19:39
    关注

    them not supporting this is baffling. You must hack the stream.

    $xml->node = '<![CDATA[character data]]>'; 
    echo preg_replace('/\]\]&gt;</', ']]><', preg_replace('/&lt;!\[CDATA/','<![CDATA',  $xml->asXML()));
    

    This is problematic, perhaps those strings might show up in your cdata somewhere. A risk you'll have to take.

    Sorry I couldn't help you with your deadline, but at least anyone annoyed with using DOM or unable to use it will have a solution of sorts.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿