php - 使用 PHP DOM 的具有混合内容的 XML 节点

标签 php xml dom

有没有办法创建一个将 XML 内容与 PHP DOM 混合的节点?

最佳答案

如果我理解正确的话,你想要类似于 JavaScript 中的innerHTML 的东西。有一个解决方案:

$xmlString = 'some <b>mixed</b> content';

$dom = new DOMDocument;
$fragment = $dom->createDocumentFragment();
$fragment->appendXML($xmlString);
$dom->appendChild($fragment);
// done

总结一下。您需要的是:

尽管您没有询问,但我会告诉您如何获取 DOM 节点的字符串表示形式,而不是整个 DOM 文档:

// for a DOMDocument you have
$dom->save($file);
$string = $dom->saveXML();

$dom->saveHTML();
$string = $dom->saveHTMLFile($file);

// For a DOMElement you have
$node = $dom->getElementById('some-id');

$string = $node->C14N();
$node->C14NFile($file);

这两种方法目前尚未记录。

关于php - 使用 PHP DOM 的具有混合内容的 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/598829/

相关文章:

xml - 在 pom.xml 中包含另一个 xml 文件

python - 如何让 Beautifulsoup 不添加 <html> 或 <?xml ?>

html - XSL-FO 生成的 PDF 中的图像宽度

php - 我如何在 PHP 中遍历 DOM 元素?

javascript - 如何让 jquery 选择已使用 client.open 加载的 html 中的类

php - 如何在 codeigniter 中应用 css 样式

javascript - file_put_content 数组结构不起作用

php - 特殊条件下的preg_match_all子域

php - 我的自定义工厂类中的 OutOfBoundsException

JavaScript - 如果满足条件,则将内容包装在 DIV 中