• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

addElement in XML

 
vidhi sanghavi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!



I am trying to parse an XML using org.dom4j.io.SAXReader.

My xml is as follows:



<record>

<item name="score">
<value>
<item name="cricket">
<value>25/1</value>
</value>
</item>
</record>
while parsing i need to add 2 more child elements ie tag with attribute tennis and other with attribute football to record tag

one child element will be before cricket and one after cricket


I want my xml to be as follows:


<record>

<item name="score">
<value>
<item name="tennis">
<value>6-3,0-2</value>

<item name="cricket">
<value>25/1</value>

<item name="football">
<value>0-0</value>
</value>
</item>
</record>


How do I do that?



Thanks in advance,

Vidhi
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you eventually end up with a DOM object, you can modify that object by using methods in the org.w3c.dom classes.

What is supposed to happen to the modified XML document? Does it get saved?

Bill
 
vidhi sanghavi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



Yes the XML document gets saved.
Which method should I use?

Thanks in advance,

Vidhi
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which method should I use?


My advice is to examine the Javadocs like I said - you will see that there are methods for locating XML Elements by name and methods for creating and adding new Nodes to Elements once you have found them.

I suggest you go work through a good XML tutorial.

Harold's book is online for free - a very complete reference.


Bill
 
vidhi sanghavi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


I have found out that insertbefore method seems to be useful for the scenario.
But I am unable to understand how to use that method.


Please help me....



Thank you in advance,
Vidhi
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Vidhi,

check if this thread can help you. It was discussion to remove a element and add a new one. Hope that helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic