DOMNode->insertBefore()

(no version information, might be only in CVS)

DOMNode->insertBefore() --  Adds a new child before a reference node

说明

class DOMNode {

DOMNode insertBefore ( DOMNode newnode [, DOMNode refnode] )

}

This function inserts a new node right before the reference node. If you plan to do further modifications on the appended child you must use the returned node.

参数

newnode

The new node.

refnode

The reference node. If not supplied, newnode is appended to the children.

返回值

The inserted node.

异常

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if this node is readonly or if the previous parent of the node being inserted is readonly.

DOM_HIERARCHY_REQUEST_ERR

Raised if this node is of a type that does not allow children of the type of the newnode node, or if the node to append is one of this node's ancestors or this node itself.

DOM_WRONG_DOCUMENT_ERR

Raised if newnode was created from a different document than the one that created this node.

DOM_NOT_FOUND

Raised if refnode is not a child of this node.


add a note add a note User Contributed Notes
26-Aug-2005 02:34
moving an existing node within the DomDocument:
<root>
  <parent>
   <child><name>Bob</name></child>
   <child><name>Sue</name></child>
  </parent>
</root>

$dom = new DomDocument();
$dom->load("sample.xml");

$parent_path = "/root";
$query = "//child[position() = 2]";
$query2 = "//child[position() = 1]";

$xpath = new DomXPath($dom);

$parent = $xpath->query($parent_path);

$under = $xpath->query($query);

$above = $xpath->query($query2);

$parent->item(0)->insertBefore($under->item(0), $above->item(0));

$dom->save("sample.xml");

Should produce:

<root>
  <parent>
   <child><name>Sue</name></child>
   <child><name>Bob</name></child>
  </parent>
</root>
jg at handcode dot de
19-Aug-2005 03:18
example to insert <newnode/> between <chid1/> and <child2/>

<?xml version="1.0" encoding="ISO-8859-1" ?>   
<root>
  <parent>
   <child nr="1"/>
   <child nr="2"/>
  </parent>
</root>

<?php
 
$xml_src
= 'test.xml';
 
// XPath-Querys
$parent_path = "//parent";
$next_path = "//parent/child[@nr='2']";
 
// Create a new DOM document
$dom = new DomDocument();
$dom->load($xml_src);
 
// Find the parent node
$xpath = new DomXPath($dom);
 
// Find parent node
$parent = $xpath->query($parent_path);
 
// new node will be inserted before this node
$next = $xpath->query($next_path);
 
// Create the new element
$element = $dom->createElement('newnode');
 
// Insert the new element
$parent->item(0)->insertBefore($element, $next->item(0));
 
echo
$dom->saveXML();
 
?>
Jerry Ellis
04-Jun-2005 03:45
1st argument) a node to insert
2nd argument) a reference node - this is the node that the new node will be inserted before

The trick to using this method is that the OBJECT on which you actually CALL the insertBefore() method is actually the PARENT node of the reference node! 

INCORRECT:
$DOMNode_refNode->insertBefore($DOMNode_newNode, $DOMNode_refNode);

CORRECT:
$DOMNode_refNode->parentNode->insertBefore($DOMNode_newNode, $DOMNode_refNode);