File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes problem in add element in xml file using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "problem in add element in xml file using java " Watch "problem in add element in xml file using java " New topic

problem in add element in xml file using java

Shaikh Riyaz

Joined: Mar 08, 2005
Posts: 20
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 ------>
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);
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");

System.out.println("----------------->>>>>>>>"+root+ "," +n.appendChild(root)+ "," +n);

}catch(Exception e)



my xml file is national.xml

<?xml version="1.0" encoding="UTF-8"?>

i am unable to add <couthrythree> in xml file

plzzzzzzzzz solve it
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
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.
I agree. Here's the link:
subject: problem in add element in xml file using java
jQuery in Action, 3rd edition