<?xml version="1.0" encoding="UTF-8"?> <person> <student> <name>zhangsan</name> <age>20</age> <sex>male</sex> </student> <student> <name>lisi</name> <age>21</age> <sex>male</sex> </student> <student> <name>wangwu</name> <age>22</age> <sex>female</sex> </student> </person> DOM: package com.dom; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class DomTest { public static void main(String[] args)throws Exception { //step1: 获取解析工厂 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // step2: 获取具体的解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // step3: 通过解析器获取文档实例 Document Document document = db.parse(new File("src/com/dom/person.xml")); // step4: 获取节点列表 NodeList list = document.getElementsByTagName("student"); for(int i=0; i<list.getLength(); i++){ Element element = (Element)list.item(i); NodeList nameList = element.getElementsByTagName("name"); for(int j=0; j<nameList.getLength(); j++){ Element eleName = (Element)nameList.item(j); String name = eleName.getFirstChild().getNodeValue(); System.out.println(name); } } } } SAP: package com.dom; import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxTest { public static void main(String[] args)throws Exception { // step1: 获取解析工厂 SAXParserFactory spf = SAXParserFactory.newInstance(); // step2: 获取解析器 SAXParser parser = spf.newSAXParser(); // setp3: 使用解析器解析文档 parser.parse(new File("src/com/dom/person.xml"), new MyHandle()); // 注册监听 } } class MyHandle extends DefaultHandler{ @Override public void characters(char[] ch, int start, int length) throws SAXException { String value = new String(ch,start,length); if(value.trim() != ""){ System.out.println("元素的值: "+value.length()+" -->" + value); } } @Override public void startDocument() throws SAXException { System.out.println("-----文档开始-----"); } @Override public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { System.out.println("====元素 "+name+" 开始===="); } @Override public void endElement(String uri, String localName, String name) throws SAXException { System.out.println("====元素 "+name+" 结束===="); } @Override public void endDocument() throws SAXException { System.out.println("-----文档开始结束-----"); } } ====实例=== package com.test; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DOMTreeTest { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("candidate.xml")); Element root = document.getDocumentElement(); parseXML(root); } public static void parseXML(Element e) { System.out.print("<" + e.getTagName()); NamedNodeMap map = e.getAttributes(); if(null != map) { for(int i = 0 ; i < map.getLength() ; ++i) { Node node = map.item(i); System.out.print(" " + node.getNodeName() + "=/"" + node.getNodeValue() + "/""); } System.out.print(">"); } NodeList list = e.getChildNodes(); for(int i = 0 ;i < list.getLength() ; ++i) { Node node = list.item(i); //表明该节点是一个元素 if(Element.ELEMENT_NODE == node.getNodeType()) { parseXML((Element)node); } else { System.out.print(node.getNodeValue()); } } System.out.print("</" + e.getNodeName() + ">"); } }