避免使用 PHP 在 xml 节点内切割标记元素
Avoid cutting markup elements within xml nodes using PHP
我想用 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.
那么我怎样才能得到整个节点,包括它的元素作为字符串?
只需使用 asXML 方法在找到的元素处获取具有根的树
foreach($paras as $para) {
echo $para->asXML();
}
我想用 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.
那么我怎样才能得到整个节点,包括它的元素作为字符串?
只需使用 asXML 方法在找到的元素处获取具有根的树
foreach($paras as $para) {
echo $para->asXML();
}