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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?