Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > appending a new node

Reply
Thread Tools

appending a new node

 
 
Kleist
Guest
Posts: n/a
 
      09-30-2005
Hello,

I use DOM XML PHP functions to build a document. Is it
possible to insert a new node as sibling right after the
selected by xpath node? There is a function insert_before()
but it seems that it appends a child?

TIA,

K.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      09-30-2005


Kleist wrote:


> I use DOM XML PHP functions to build a document. Is it possible to
> insert a new node as sibling right after the selected by xpath node?
> There is a function insert_before() but it seems that it appends a child?


In PHP 5 it should work alike
node->parentNode->insertBefore(newNode, node->nextSibling);

as in the following complete example

<?php
$xmlDocument = new DOMDocument('1.0', 'UTF-8');
$loaded = $xmlDocument->loadXML('
<gods>
<god name="Kibo" />
<god name="Xibo" />
</gods>');

$newGod = $xmlDocument->createElement('god');
$newGod->setAttribute('name', 'Maho');

$xpathEvaluator = new DOMXPath($xmlDocument);
$oldGod = $xpathEvaluator->query('/gods/god[@name = "Kibo"]',
$xmlDocument)->item(0);
if ($oldGod != NULL) {
$oldGod->parentNode->insertBefore($newGod, $oldGod->nextSibling);
}

header('Content-Type: application/xml');
echo $xmlDocument->saveXML();
?>

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Kleist
Guest
Posts: n/a
 
      09-30-2005
Martin Honnen wrote:

>
> In PHP 5 it should work alike



Unfortunally I have only PHP 4.3.1 on my server, and this
will not do. But anyway thanks for replay
 
Reply With Quote
 
Janwillem Borleffs
Guest
Posts: n/a
 
      10-02-2005
Kleist wrote:
> Unfortunally I have only PHP 4.3.1 on my server, and this
> will not do. But anyway thanks for replay
>


The following looks a bit cumbersome but appears to do the trick:

<?php

if (!$dom = domxml_open_mem('<doc><child id="0" /><child id="2" /></doc>'))
{
echo "Error while parsing the document\n";
exit;
}

$xpath = xpath_new_context($dom);
$root = $dom->document_element();

$element = $dom->create_element('child');
$element->set_attribute('id', 1);

$nodeset = xpath_eval($xpath, '//child[@id=0]');
$node = $nodeset->nodeset[0];

if ($sibling = $node->next_sibling()) {
$root->insert_before($element, $sibling);
} else {
$node->append_sibling($element);
}

echo "<pre>";
$xmlfile = $dom->dump_mem();
echo htmlentities($xmlfile);
echo "</pre>";

?>


JW



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The difference between new node() and new node? remlostime C++ 4 09-05-2008 08:16 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
how to creating new node inside a xml node Geagleeye ASP General 2 08-03-2006 12:09 PM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
How to drag a sub-node to another node in TreeView? wolf ASP .Net 0 08-18-2004 11:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57