且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP DOM的混合内容的XML节点

更新时间:2023-11-24 15:51:16

如果我有一个方法可以使用PHP DOM创建一个混合的XML内容的节点正确地理解你在JavaScript中需要类似innerHTML的东西。有一个解决方案:

  $ xmlString ='some< b> mixed< / b>内容'; 

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

进行汇总。您需要的是:





虽然你没有问过我会告诉你如何获取DOM节点的字符串表示,而不是整个DOM文档:

  //对于DOMDocument,您有
$ dom-> save($ file);
$ string = $ dom-> saveXML();

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

//对于一个DOMElement,你有
$ node = $ dom-> getElementById('some-id');

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

这两种方法目前没有记录。


Is there a way to create a node that has mixed XML content in it with the PHP DOM?

If I understood you correctly you want something similar to innerHTML in JavaScript. There is a solution to that:

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

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

To sumarize. What you need is:

Although you didn't asked about it I'll tell you how to get the string representation of a DOM node as opposed to the whole DOM document:

// 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);

Those two methods are currently not documented.