File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes appending node to xml file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "appending node to xml file" Watch "appending node to xml file" New topic
Author

appending node to xml file

Amit Rogye
Greenhorn

Joined: Sep 09, 2001
Posts: 5
how can i append node to existing xml file. if somebody can give code that will be helpful
Paramagurusamy Balasubramanian
Ranch Hand

Joined: Aug 17, 2001
Posts: 55
Hi amit,
Here is the sample code to achieve your task..
Document document=new SAXReader().read("c:/student.xml");
Element students=document.getRootElement() ;
Element student=students.addElement("student");
Element id=student.addElement("id")
.addText("2");
Element name=student.addElement("name")
.addText("name2");
Element city=student.addElement("city")
.addText("city2");
FileOutputStream f1=new FileOutputStream("c:/student.xml");
XMLWriter writer=new XMLWriter(f1);
writer.write(document);
write.flush();
f1.close();
------------
// student.xml
<?xml version="1.0" encoding="UTF-8" ?>
<students>
<student>
<id>1</id>
<name>name1</name>
<city>city1</city>
</student>
<student>
<id>2</id>
<name>name2</name>
<city>city2</city>
</student>
</students>
Hope i fulfilled your request...
Best Regards,
Paramaguru
Paramagurusamy Balasubramanian
Ranch Hand

Joined: Aug 17, 2001
Posts: 55
Hi amit,
Here is the sample code to achieve your task..
Document document=new SAXReader().read("c:/student.xml");
Element students=document.getRootElement() ;
Element student=students.addElement("student");
Element id=student.addElement("id")
.addText("2");
Element name=student.addElement("name")
.addText("name2");
Element city=student.addElement("city")
.addText("city2");
FileOutputStream f1=new FileOutputStream("c:/student.xml");
XMLWriter writer=new XMLWriter(f1);
writer.write(document);
write.flush();
f1.close();
------------
// student.xml
<?xml version="1.0" encoding="UTF-8" ?>
<students>
<student>
<id>1</id>
<name>name1</name>
<city>city1</city>
</student>
<student>
<id>2</id>
<name>name2</name>
<city>city2</city>
</student>
</students>
Hope i fulfilled your request...
Best Regards,
Paramaguru
Dipak Fatania
Greenhorn

Joined: Apr 12, 2004
Posts: 1
dim Node as xmlNode
Dim Doc As New XmlDocument()
Node = Doc.DocumentElement
Dim elem As XmlElement = Doc.CreateElement("Node Name You Want To creat")
elem.InnerText = txtName.Text
Node.InsertAfter(elem, Node.FirstChild)
Doc.Save(path)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: appending node to xml file