• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help in edit the xml using dom parser

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

[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]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

Bill
 
reply
    Bookmark Topic Watch Topic
  • New Topic