简单例子: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //support the namespace factory .setNamespaceAware(true);
//Get the inputStream of your xml file FileInputStream in = new FileInputStream(new File("xxx.xml"));
//Get the instance Document document = factory.newDocumentBuilder().parse(in);
//TODO your operation here
//You can also use the SaxParser
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); //MyHandler is a class extends DefaultHandler which use to handler the xml file factory.newSAXParser().parse(new File("xxx.xml"), new MyHandler());
//There is also other parser for special xml files. For example : SOAPMessage message = MessageFactory.newInstance().createMessage();
//The MessageFactory_Class in jdk1.6 is more powerful(support soap1.2) for soapmessage parse!
在 JAXP 的新版本中 —— 包括在 JDK 中 —— Sun 已经重新包装了 Apache Xerces 解析器http://www.ibm.com/developerworks/cn/xml/x-jaxp/
这文章写的不错。
javax.xml.parsers.*javax.xml.*jdk中的这些包 里面应该有你想要的东西! 本身就带这些 不用另外导入包
//support the namespace
factory .setNamespaceAware(true);
//Get the inputStream of your xml file
FileInputStream in = new FileInputStream(new File("xxx.xml"));
//Get the instance
Document document = factory.newDocumentBuilder().parse(in);
//TODO your operation here
//You can also use the SaxParser
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
//MyHandler is a class extends DefaultHandler which use to handler the xml file
factory.newSAXParser().parse(new File("xxx.xml"), new MyHandler());
//There is also other parser for special xml files. For example :
SOAPMessage message = MessageFactory.newInstance().createMessage();
//The MessageFactory_Class in jdk1.6 is more powerful(support soap1.2) for soapmessage parse!