readXML

readXML

View Answers

September 10, 2008 at 10:21 AM

Hi friend,

import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class xmlRead{
static public void main(String[] arg) {
try{
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter XML file name: ");
String xmlFile = buff.readLine();
File file = new File(xmlFile);
if (file.exists()){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.METHOD, "text");
Source source = new DOMSource(doc);
Result result = new StreamResult(System.out);
tf.transform(source, result);
Node node = doc.getDocumentElement();
String root = node.getNodeName();
System.out.println("Root Node: " + root);
}
else{
System.out.println("File not found!");
}
}
catch (Exception e){
System.err.println(e);
System.exit(0);
}
}
}

---------------------------------------

For read more information at:

http://www.roseindia.net/xml/

Thanks & regards

Amardeep









Related Tutorials/Questions & Answers:
readXML - XML
readXML - XML
Advertisements
xml - XML
javax.xml.transform.stream.*; public class ReadXML { public static void main(String args..."); FileOutputStream output = new FileOutputStream("new.xml"); ReadXML xml = new ReadXML(); xml.convert(input, output); System.out.println("XML File
read XML file and display it using java servlets
+"')"); readXML = new XmlBean(userName,url); String str = fw.fileWrite(xmlpath,readXML); system.out.println(str

Ads