Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » add element to xml(adding new elements to xml using java)
add element to xml [message #896539] Wed, 18 July 2012 18:55 Go to next message
anijdap ykus is currently offline anijdap ykus
Messages: 4
Registered: July 2012
Junior Member
Hello Pals!


Here is my initial xml file:

----------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<missions>
<mission number="1">
<name>first mission</name>
<score>1.0</score>
</mission>
<mission number="2">
<name>second mission</name>
<score>0.2</score>
</mission>
</missions>
----------------------------------------

I am trying to add a new element "mission" to my xml file.

here is a bit of my code:
--------------------------------------------------------------------
public class ModifyXMLFile {
public static void main(String[] args) {

try {

SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("c:\\file3.xml");

Document doc = (Document)builder.build(xmlFile);

Element missions = doc.getRootElement();

Element newMission= new Element("newMission");

missions.addContent(newMission);

// I want to add an attribute to newMission trying:
/* Attr number = doc.createAttribute("number");
number.setValue("3");
mission.setAttributeNode(number);
*/
--------------------------------------------------------------------
The above commented code does obviously not work. Any idea on how to get what I want?
I want the final xml to look like:
----------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<missions>
<mission number="1">
<name>first mission</name>
<score>1.0</score>
</mission>
<mission number="2">
<name>second mission</name>
<score>0.2</score>
</mission>
<newMission number="3">
<name>second mission</name>
<score>0.2</score>
</mission>
</missions>
-----------------------------------------------
what the functional part of my code gives me is:
-----------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<missions>
<mission number="1">
<name>first mission</name>
<score>1.0</score>
</mission>
<mission number="2">
<name>second mission</name>
<score>0.2</score>
</mission>
<newMission />
</missions>
-----------------------------------------------

I am not sure how to handle this.

Thanks in advance
Re: add element to xml [message #896548 is a reply to message #896539] Wed, 18 July 2012 19:54 Go to previous message
Russell Bateman is currently offline Russell Bateman
Messages: 3221
Registered: July 2009
Location: Provo, Utah, USA
Senior Member

On 7/18/2012 12:55 PM, padina suky wrote:
> Hello Pals!
>
> Here is my initial xml file:
>[snip]
> I am not sure how to handle this.
>
> Thanks in advance

Padina,

This isn't a general programming help forum. It's for questions
regarding the set-up and use of Eclipse. You'll want to ask this
question elsewhere. I'd suggest stackoverflow.com.

Russ
Previous Topic:import org.jdom?
Next Topic:Bpel Workflows web services with Lego NXT Brick
Goto Forum:
  


Current Time: Thu Oct 23 16:01:31 GMT 2014

Powered by FUDForum. Page generated in 0.01705 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software