DOM|SAX|JDOM
DOM
树模型,
/* parse入参其他形式 eg:"cof/11.xml"
* 1. File file = new File(filepath)
* 2. InputStream xmlIns = new FileInputStream(filepath)
* 3. InputSource is = new InputSource(filepath)
* 4. InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filepath)
* 5. ByteArrayInputStream bais = new ByteArrayInputStream(xml_format_str.getBytes())//适应于xml格式的字符串 */
public static Document getDocument(String filepath) throws ParserConfigurationException, IOException, SAXException {
//创建DOM解析器的工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//DOM解析器对象
DocumentBuilder builder = factory.newDocumentBuilder();
//解析XML文档得到整个Document对象
Document doc = builder.parse(filepath);
return doc;
}
其中,1-3默认类文件相对路径,getResourceAsStream()
默认从ClassPath根下获取,不能以/开头
Element root = doc.getDocumentElement();
NodeList list = doc.getElementsByTagName("nodename");
SAX
"推"式流模型,
JDOM
Java-based Document Object Model,树模型
DOM4J
Document Object Model for Java
StAX
Streaming API for XML:XmlStreamReader + XmlStreamWriter
,"拉"式流模型,JDK1.6新特性