Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding elements at the same level of an XML document

 
John Paul Hoey
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You 're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic