It's not a secret anymore!*
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
Author

need help in edit the xml using dom parser

narasimha java
Greenhorn

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.

[code]

import java.io.File;

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 javax.xml.transform.stream.StreamResult;
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) {
try{
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();
System.out.println("document-------"document);
Element ele= document.getDocumentElement();
NodeList nl=ele.getElementsByTagName("Emp_Name");
System.out.println("length------------"+nl.getLength());
for (int k=0;k<nl.getLength();k+)
{
Element nod=(Element)nl.item(k);
String s1=nod.getTextContent();
System.out.println("---------------"+s1);
nod.setTextContent("rahul");
}
Source src = new DOMSource(ele);
Result dest = new StreamResult(file);
transfor.transform(src, dest);
}catch(Exception e)
{e.printStackTrace();}

}

}
[code]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12836
    
    5
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.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help in edit the xml using dom parser