Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need help in edit the xml using dom parser  RSS feed

narasimha java
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!