473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

appending a new node

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.
Sep 30 '05 #1
3 1489


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/
Sep 30 '05 #2
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
Sep 30 '05 #3
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

Oct 2 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Johnny | last post by:
I have below a sample parent-child xml that I'm trying to manipulate <Persons> <Person Id='1'/> </Persons> I will have a string that I want to append to my existing xml shown below:
1
by: Novice | last post by:
Hey all, I've read a few articles about speed and XML processing - so I just want to make sure that I'm using the right strategy for what I want to achieve. I have an XML file that I'm appending...
1
by: Jonathan Taylor | last post by:
I have a large XML file, that is too large to read in to XmlDocument. I need to append data to this XML file without creating a new file, since I don't want to have two copies of the large file...
17
by: Grant Austin | last post by:
I have a linked list where each node holds a couple numbers a char and a const char array. I use strcpy to take an char array argument passed to my insert function to copy the string into the new...
4
by: Jesper Stocholm | last post by:
I have a database class that maintains data about customers i my system. The basic XML for this looks like: <Chunk> <Vendor> <Database/> </Vendor> </Chunk> When a user is to be registrered...
2
by: tony.collings | last post by:
I started a thread here : http://groups.google.co.uk/group/microsoft.public.cmserver.general/browse_thread/thread/29d63077144004a9/c3888efdcb7338f6?hl=en#c3888efdcb7338f6 About creating an Email...
2
by: Shaurya Vardhan | last post by:
Hi, On Appending a child in XML Node, I am having error, "Run-time exception thrown : System.ArgumentException - The node to be inserted is from a different document context." How to resolve...
1
by: luthriaajay | last post by:
I have a tricky question here. 1).I have an XML that is generated on the fly. XML Generated ------------- Document genereatedDoc =
0
by: ghafarifar | last post by:
How I appending a new node to am existing xml file? help?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.