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標籤(斜體&黑體)的元素。 在此先感謝