wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes addElement in XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "addElement in XML " Watch "addElement in XML " New topic
Author

addElement in XML

vidhi sanghavi
Greenhorn

Joined: Mar 28, 2009
Posts: 13
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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Mar 28, 2009
Posts: 13
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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Mar 28, 2009
Posts: 13
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

Joined: Jan 01, 2009
Posts: 874



Vidhi,

check if this thread can help you. It was discussion to remove a element and add a new one. Hope that helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: addElement in XML
 
Similar Threads
Indian cricket team in Australia
To all cricket lovers out there
Enjoy these cricket... classics...!!
Cricket Question
[Cricket] New coach for India