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);DOMNode::insertBefore
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
DOMNode::insertBefore
Референца за `domnode.insertbefore.php` со подобрена типографија и навигација.
DOMNode::insertBefore
класата mysqli_driver
DOMNode::insertBefore — Додава ново дете пред референтен јазол
= NULL
Оваа функција вметнува нов јазол веднаш пред референтниот јазол. Ако планирате да направите дополнителни модификации на додаденото дете, мора да го користите вратениот јазол.
Кога користите постоечки јазол, тој ќе биде преместен.
Параметри
node-
Новиот јазол.
child-
Референтниот јазол. Ако не е обезбеден,
nodeсе додава на децата.
Вратени вредности
Вметнатиот јазол или false при грешка.
Errors/Exceptions
Може да фрли DOMException со следните кодови за грешки:
DOM_NO_MODIFICATION_ALLOWED_ERR-
Покренато ако овој јазол е само за читање или ако претходниот родител на јазолот што се вметнува е само за читање.
DOM_HIERARCHY_REQUEST_ERR-
Покренато ако овој јазол е од тип што не дозволува деца од типот на
nodeјазол, или ако јазолот што треба да се додаде е еден од предците на овој јазол или самиот овој јазол. DOM_WRONG_DOCUMENT_ERR-
Покренато ако
nodeе создаден од различен документ од оној што го создал овој јазол. DOM_NOT_FOUND_ERR-
Покренато ако
childне е дете на овој јазол.
Види Исто така
- . This node will not show up in the document unless it is inserted with (e.g.) - Додава ново дете на крајот од децата
Белешки од корисници 4 белешки
Note that supplying the same node for $newnode and $refnode leads to an E_WARNING ("Couldn't add newnode as the previous sibling of refnode"). For example imagine one wanted to make $newnode the first child of its parent by doing:
<?php
$firstSibling = $newnode->parentNode->firstChild;
// Bad:
$newnode->parentNode->insertBefore( $newnode, $firstSibling );
?>
This would generate a warning if it already was the first child of its parent, since $newnode and $firstSibling are identical. Easy to work around though:
<?php
$firstSibling = $newnode->parentNode->firstChild;
// Better:
if( $newnode !== $firstSibling ) {
$newnode->parentNode->insertBefore( $newnode, $firstSibling );
}
?>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();
?>Sorry, my previous posting worked only for the top node. Here the corrected version, which will work for any node:
XML
----
<?xml version="1.0"?>
<contacts>
<person>Adam</person>
<person>Eva</person>
<person>Thomas</person>
</contacts>
PHP
---
<?php
// load XML, create XPath object
$xml = new DomDocument();
$xml->preserveWhitespace = false;
$xml->load('contacts.xml');
$xpath = new DOMXPath($xml);
// get node eva, which we will append to
$eva = $xpath->query('/contacts/person[.="Eva"]')->item(0);
// create node john
$john = $xml->createElement('person', 'John');
// insert john after eva
// "in eva's parent node (=contacts) insert
// john before eva's next node"
// this also works if eva would be the last node
$eva->parentNode->insertBefore($john, $eva->nextSibling);
// show result
header('Content-Type: text/plain');
print $xml->saveXML();
?>
Result
------
<?xml version="1.0"?>
<contacts>
<person>Adam</person>
<person>Eva</person><person>John</person>
<person>Thomas</person>
</contacts>