This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

problem in add element in xml file using java

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
there is some problem in my application it is unable to add the element in my xml file plzzzzzz help me.
My java file is
Geography.java ------>
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GeographyExample {


public static void main (String argv[]){
try {

File fl=new File("national.xml");
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(fl);
NodeListnl=doc.getElementsByTagName("country");
System.out.println("###########" +nl);
int i = nl.getLength();
for(int j=0 ; j<i;j++){
Node n = nl.item(j);
//Node parent = n.getParentNode();
Element root = doc.createElement("countrythree");

n.appendChild(doc.createTextNode("countrythree"));
System.out.println("----------------->>>>>>>>"+root+ "," +n.appendChild(root)+ "," +n);
}



}catch(Exception e)
{
System.out.println(e);

}

}
}

my xml file is national.xml

<?xml version="1.0" encoding="UTF-8"?>
<geography>
<country>
<countryone>U.S.A</countryone>
<countrytwo>U.K</countrytwo>
</country>
</geography>

i am unable to add <couthrythree> in xml file

plzzzzzzzzz solve it
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like you have not made provision for writing out the modified XML document. You have modified the DOM in memory but that will not change the disk file automatically. The tools used to write a DOM depend on the Java version and/or outside toolkit you are using.
IF you have Java 1.4 or 1.5 - look at the javax.xml.transform package.
Any XML tutorial will have example code.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!