简体   繁体   中英

XML node with Mixed Content using PHP DOM

有没有办法创建一个在其中将XML内容与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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM