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 need help in edit the xml using dom parser 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 "need help in edit the xml using dom parser " Watch "need help in edit the xml using dom parser " New topic

need help in edit the xml using dom parser

narasimha java

Joined: Sep 22, 2009
Posts: 1
Dear All,

I am new to xml technology so i hope you can help me.the problem is ...
i have an xml file which i need to edited some elements values like let xml file is like this i want to change Emp_Name to Rahul

after completion of the coding i hvae noticed that i could not write document or element to console or file and after parsing if i see the document object it is showing null. i have stucked here ...

can you please help me......

here i am sending the code which i followed.



import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import org.xml.sax.helpers.DefaultHandler;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class finaltest {

@param arg
public static void main(String[] args) {
File file = new File("C:\\temp.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
TransformerFactory tranFact = TransformerFactory.newInstance();
Transformer transfor = tranFact.newTransformer();
Element ele= document.getDocumentElement();
NodeList nl=ele.getElementsByTagName("Emp_Name");
for (int k=0;k<nl.getLength();k+)
Element nod=(Element)nl.item(k);
String s1=nod.getTextContent();
Source src = new DOMSource(ele);
Result dest = new StreamResult(file);
transfor.transform(src, dest);
}catch(Exception e)


William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
1. ALWAYS use the "code" button to enclose code so humans can read it.
2. The toString() value of a org.w3c.dom.Document is always null. Before doing any more xml programming, carefully study the table in the JavaDocs for org.w3c.dom.Node interface. This shows what each type of DOM Node will return for name and value.

I agree. Here's the link:
subject: need help in edit the xml using dom parser
It's not a secret anymore!