aspose file tools*
The moose likes XML and Related Technologies and the fly likes Adding elements at the same level of an XML document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Adding elements at the same level of an XML document" Watch "Adding elements at the same level of an XML document" New topic
Author

Adding elements at the same level of an XML document

John Paul Hoey
Greenhorn

Joined: Apr 15, 2012
Posts: 25
Hi all,

I was wondering if someone could help me with my query please.

I have an XML document very similar to the following (this is just a snippet):



What i want to do is have my programme create a few lines of XML i.e <LevelOneB> between <LevelOneA> and <LevelOneC> so that the eventual XML looks like this:



The problem i have however is that the code i have written adds <LevelOneB> AFTER <LevelOneC> (which i suppose makes sense as im using the .appendChild() method).

This is what i get as the outcome:




Here is a snippet of the code that im currently using:




Ive been at this all night and I cant seem to figure out how to do this therefore if anyone could help me with my problem that would be very much appreciated.

Regards

John
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Consider the insertBefore() method in the org.w3c.dom.Node class.

My step will be to find Node C (LevelOneC), and insert the Level B node just before it.

John Paul Hoey
Greenhorn

Joined: Apr 15, 2012
Posts: 25
Hi John,

Thanks for the reply and apologies it has taken me a few days to respond.

I implemented the advice you have given and it worked successfully.

I had actually tried to use the .insertBefore() but being a relatively novice at Java i wasnt sure how it would have worked ... and by looking at your supplied code i was actually missing the .getParentNode() part of it.

So once again thanks for your help which is very much appreciated

Regards

John Paul
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You 're welcome.
 
wood burning stoves
 
subject: Adding elements at the same level of an XML document