Inserting a new line using DOM
I'm attempting to insert a new node into an XML document. I'm using simpleXML for most of the parsing but for this piece I need to use DOM. This is the function i'm using to make the addition
function simplexml_insert_after(SimpleXMLElement $sxe, SimpleXMLElement $insert, SimpleXMLElement $target)
{
$target_dom = dom_import_simplexml($target)开发者_如何学编程;
$target_dom->formatOutput = true;
$target_dom->preserveWhiteSpace = false;
$insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
if ($target_dom->nextSibling) {
return $target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
} else {
return $target_dom->parentNode->appendChild($insert_dom);
}
}
And calling it like this:
$meta2 = new SimpleXMLElement("<meta/>");
$meta2->addAttribute('name', 'subject');
$meta2->addAttribute('value', $doc_id);
$target = current($my_xml->xpath('//meta[last()]'));
simplexml_insert_after($my_xml, $meta2, $target);
My Problem is that its inserting the new node immediately after the target rather than on a new line so the resulting XML looks like this:
<meta content="a" name="ap-category"/>
<meta content="bx" name="ap-format"/><meta name="subject" value="urn:blah"/>
When I'd like it to look like this:
<meta content="a" name="ap-category"/>
<meta content="bx" name="ap-format"/>
<meta name="subject" value="urn:blah"/>
I've tried changing preserveWhiteSpace to true, but it did not help. How do i go about adding a new line before inserting that node?
edit Here is the fix that worked:
function simplexml_insert_after(SimpleXMLElement $sxe, SimpleXMLElement $insert,SimpleXMLElement $target)
{
$target_dom = dom_import_simplexml($target);
$target_dom->formatOutput = true;
$target_dom->preserveWhiteSpace = false;
$insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
if ($target_dom->nextSibling) {
$result = $target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
$target_dom->parentNode->insertBefore($target_dom->ownerDocument->createTextNode("\n"), $result);
return $result
} else {
return $target_dom->parentNode->appendChild($insert_dom);
}
}
Try to insert a textnode just before the element you want to insert, something like this:
function simplexml_insert_after(SimpleXMLElement $sxe,
SimpleXMLElement $insert,
SimpleXMLElement $target)
{
$target_dom = dom_import_simplexml($target);
$target_dom->formatOutput = true;
$target_dom->preserveWhiteSpace = false;
$document = $target_dom->ownerDocument;
$insert_dom = $document->importNode(dom_import_simplexml($insert), true);
$parentNode = $target_dom->parentNode;
if ($target_dom->nextSibling) {
$result = $parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
$parentNode->insertBefore($document->createTextNode("\n"), $result);
return $result
} else {
return $parentNode->appendChild($insert_dom);
}
}
Try changing your method call to this:
simplexml_insert_after($my_xml, "\n" . $meta2, $target);
...with preserveWhiteSpace = true;
精彩评论