douwen3973 2017-11-23 14:36
浏览 50
已采纳

避免使用PHP在xml节点中删除标记元素

I want to parse a DocBook XML file with PHP. SimpleXMLElement cuts off some markup elements.

<section>
<title>SUSPEND</title>
    <para>The <database>SUSPEND</database> command <quote>breaks</quote> the atomicity of the block in which it is located.</para>
    <para>...</para>
</section>

I use this code to parse my para elements:

$paras = $xml->xpath("//*[starts-with(local-name(), 'para')]");
foreach($paras as $para) {
   echo $para[0];
}

This cuts off my markup

<database>SUSPEND</database> and <quote>breaks</quote>

resulting in

The command the atomicity of the block in which it is located.

But what I need is:

The <database>SUSPEND</database> command <quote>breaks</quote> the atomicity of the block in which it is located.

So how can I get the whole node including its elements as string?

  • 写回答

1条回答 默认 最新

  • duanqie5741 2017-11-23 14:56
    关注

    Just use asXML method to get tree with root at element found

    foreach($paras as $para) {
       echo $para->asXML();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥70 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52