douwen3973 2017-11-23 06: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 06:56
    关注

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

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部