Java解析XML文档
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class loadXML {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
loadXML load = new loadXML("drivers.xml");
load.loadDocument();
}
private String strFileName = "";
public loadXML(String fileName)
{
this.strFileName = fileName;
}
private void loadDocument()
{
File file = new File(strFileName);
try{
InputStream in = new FileInputStream(file);
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(in);
document.getDocumentElement().normalize();
Element rootElement = document.getDocumentElement();
NodeList driverElements =
rootElement.getElementsByTagName("driver");
for(int i=0;i<driverElements.getLength();i++)
{
Node node = driverElements.item(i);
NodeList children = node.getChildNodes();
for(int j=0;j<children.getLength();j++)
{
Node child = children.item(j);
String nodeName = child.getNodeName();
if(child instanceof Element)
{
Node textNode = child.getChildNodes().item(0);
System.out.println("----------------" + nodeName + "-----------------" + textNode.getNodeValue());
}
}
}
}catch (FileNotFoundException fnfe){
System.err.println("file error!");
}catch (ParserConfigurationException pce){
System.err.println("DocumentBuilder error!");
}catch (SAXException sa){
System.err.println("Document error!");
}catch (IOException ioe){
System.err.println("IO error!");
}
}
}
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class loadXML {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
loadXML load = new loadXML("drivers.xml");
load.loadDocument();
}
private String strFileName = "";
public loadXML(String fileName)
{
this.strFileName = fileName;
}
private void loadDocument()
{
File file = new File(strFileName);
try{
InputStream in = new FileInputStream(file);
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(in);
document.getDocumentElement().normalize();
Element rootElement = document.getDocumentElement();
NodeList driverElements =
rootElement.getElementsByTagName("driver");
for(int i=0;i<driverElements.getLength();i++)
{
Node node = driverElements.item(i);
NodeList children = node.getChildNodes();
for(int j=0;j<children.getLength();j++)
{
Node child = children.item(j);
String nodeName = child.getNodeName();
if(child instanceof Element)
{
Node textNode = child.getChildNodes().item(0);
System.out.println("----------------" + nodeName + "-----------------" + textNode.getNodeValue());
}
}
}
}catch (FileNotFoundException fnfe){
System.err.println("file error!");
}catch (ParserConfigurationException pce){
System.err.println("DocumentBuilder error!");
}catch (SAXException sa){
System.err.println("Document error!");
}catch (IOException ioe){
System.err.println("IO error!");
}
}
}