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

时间:2017-11-23 14:36:17

标签: php xml docbook-5

我想用PHP解析DocBook XML文件。 SimpleXMLElement会切断一些标记元素。

<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>

我使用此代码来解析我的para元素:

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

这会切断我的标记

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

导致

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

但我需要的是:

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

那么如何将整个节点包含其元素作为字符串?

1 个答案:

答案 0 :(得分:1)

只需使用asXML方法在找到的元素

中获取root用户树
foreach($paras as $para) {
   echo $para->asXML();
}