1
我有一个看起来像这样的XML:PHP的XML DOM - 更新XML DOM元素
<nitf:body.content>
<nitf:block>
<nitf:p style="@style1">Contents of paragraph1.</nitf:p>
<nitf:p style="@style2">Contents of paragraph2.</nitf:p>
<nitf:p style="@style1"><nitf:em class="@bold">This is bold</nitf:em> This is not bold</nitf:p>
<nitf:p style="@style1"><nitf:em class="@italic">This is italic</nitf:em> This is not italic</nitf:p>
</nitf:block>
</nitf:body.content>
而且我做了一个循环更新所有NITF文:EM标签如下:
foreach($this->doc->getElementsByTagNameNS($this->nitfNS, 'em') as $em) {
$class = $em->getAttribute('class');
if ($class == '@italic') {
$em->nodeValue = '<i>' . $em->nodeValue . '</i>';
}
elseif (strpos($class, 'bold') !== FALSE) {
$em->nodeValue = '<b>' . $em->nodeValue . '</b>';
}
$this->doc->saveXML($em);
}
现在,当我再次遍历段落元素时,应该由前一个循环更新的段落全部为空。
foreach ($this->doc->getElementsByTagNameNS($this->nitfNS, 'p') as $element) {
$textnode = $element->childNodes->item(0);
$txt = $textnode->wholeText; // this is EMPTY now
}
我某处读到 “<>” 字符可能搞乱了DOM解析器。如果在这种情况下,我如何更新所需html标签(斜体&黑体)的元素。 在此先感谢