Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

appending node to xml file

 
Amit Rogye
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i append node to existing xml file. if somebody can give code that will be helpful
 
Paramagurusamy Balasubramanian
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic