Dom解析 Dom4j解析是一次性将文件读取到内存中来进行解析,而SAX是事件驱动型,读一行解析一行。
SAXReader 解析器
1 import java.util.Iterator; 2 3 import org.dom4j.Document; 4 import org.dom4j.Element; 5 import org.dom4j.io.SAXReader; 6 7 public class Dom4jTest { 8 9 /** 10 * @param args 11 */ 12 public static void main(String[] args) { 13 try { 14 //创建解析器 15 SAXReader reader = new SAXReader(); 16 //通过解析器的read方法将配置文件读取到内存中,生成一个Document[org.dom4j]对象树 17 Document document = reader.read("conf/students.xml"); 18 //获取根节点 19 Element root = document.getRootElement(); 20 //开始遍历根节点 21 for(Iterator<Element> rootIter = root.elementIterator();rootIter.hasNext();){ 22 Element studentElt = rootIter.next(); 23 for(Iterator<Element> innerIter = studentElt.elementIterator();innerIter.hasNext();){ 24 Element innerElt = innerIter.next(); 25 String innerValue = innerElt.getStringValue(); 26 System.out.println(innerValue); 27 } 28 System.out.println("-------------------------------"); 29 } 30 } catch (Exception e) { 31 e.printStackTrace(); 32 } 33 } 34 35 }
//sax解析
1 import javax.xml.parsers.ParserConfigurationException; 2 import javax.xml.parsers.SAXParser; 3 import javax.xml.parsers.SAXParserFactory; 4 5 import org.xml.sax.Attributes; 6 import org.xml.sax.SAXException; 7 import org.xml.sax.helpers.DefaultHandler; 8 9 public class MySAXParser { 10 11 /** 12 * @param args 13 */ 14 public static void main(String[] args) { 15 try { 16 //创建解析器工厂 17 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 18 //创建解析器 19 SAXParser saxParser = saxParserFactory.newSAXParser(); 20 //通过解析器的parser方法 21 saxParser.parse("conf/persons.xml", new MyDefaultHandler()); 22 } catch (Exception e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 } 27 28 } 29 30 class MyDefaultHandler extends DefaultHandler{ 31 32 @Override 33 public void startElement(String uri, String localName, String qName, 34 Attributes attributes) throws SAXException { 35 System.out.print("<" + qName + ">"); 36 } 37 @Override 38 public void characters(char[] ch, int start, int length) 39 throws SAXException { 40 System.out.print(new String(ch,start,length)); 41 } 42 43 @Override 44 public void endElement(String uri, String localName, String qName) 45 throws SAXException { 46 System.out.print("</" + qName + ">"); 47 } 48 49 50 51 }
1 Xpath解析 2 import org.dom4j.Document; 3 import org.dom4j.Element; 4 import org.dom4j.io.SAXReader; 5 6 public class SysConfigParser { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 try { 13 //创建解析器 14 SAXReader reader = new SAXReader(); 15 //通过解析器的read方法将配置文件读取到内存中,生成一个Dcoumente【org.dom4j】对象树 16 Document document = reader.read("conf/sys-config.xml"); 17 //driver-name节点元素的路径:config -> database-info -> driver-name 18 // driver-name节点元素的xpath路径:/config/database-info/driver-name 19 Element driverNameElt = (Element) document.selectSingleNode("/config/database-info/driver-name"); 20 //获取driverNameElt节点元素对象的文本内容 21 String driverName = driverNameElt.getStringValue(); 22 System.out.println(driverName); 23 24 //url节点元素的路径: config -> database-info -> url 25 //url节点元素的xpath路径: /config/database-info/url 26 //url节点元素的xpath路径: config//url 27 //url节点元素的xpath路径: //url 28 Element urlElt = (Element) document.selectSingleNode("config//url"); 29 String url = urlElt.getStringValue(); 30 System.out.println(url); 31 32 //user节点元素的路径:config -> database-info -> user 33 //user节点元素的xpath路径:/config/database-info/user 34 //user节点元素的xpath路径:config//user 35 //user节点元素的xpath路径://user 36 Element userElt = (Element) document.selectObject("//user"); 37 String user = userElt.getText(); 38 System.out.println(user); 39 40 Element passwordElt = (Element) document.selectSingleNode("//password"); 41 String password = passwordElt.getTextTrim(); 42 System.out.println(password); 43 } catch (Exception e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } 47 } 48 49 }
1 import org.dom4j.Attribute; 2 import org.dom4j.Document; 3 import org.dom4j.DocumentException; 4 import org.dom4j.Element; 5 import org.dom4j.io.SAXReader; 6 7 public class ServerParser { 8 9 /** 10 * @param args 11 */ 12 public static void main(String[] args) { 13 try { 14 //创建解析器 15 SAXReader saxReader = new SAXReader(); 16 //通过解析器的read方法将配置文件读取到内存中,生成一个Document对象树 17 Document document = saxReader.read("conf/server.xml"); 18 //获取connector节点元素对象的路径:server -> service -> connector 19 //获取connector节点元素对象的xpath路径:/server/service/connector 20 //获取connector节点元素对象的xpath路径:server//connector 21 //获取connector节点元素对象的xpath路径://connector 22 Element connectorElt = (Element) document.selectSingleNode("//connector"); 23 24 //获取connectorElt节点元素对象的port属性对象 25 Attribute portAttr = connectorElt.attribute("port"); 26 //获取portAttr属性对象的值 27 String port = portAttr.getStringValue(); 28 29 String portValue = connectorElt.attributeValue("port"); 30 31 System.out.println(portValue); 32 33 } catch (Exception e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 } 38 39 }
1 import javax.xml.parsers.DocumentBuilder; 2 import javax.xml.parsers.DocumentBuilderFactory; 3 import javax.xml.parsers.ParserConfigurationException; 4 import javax.xml.xpath.XPath; 5 import javax.xml.xpath.XPathConstants; 6 import javax.xml.xpath.XPathFactory; 7 8 import org.w3c.dom.Document; 9 import org.w3c.dom.Element; 10 import org.w3c.dom.NodeList; 11 12 public class MyXPathTest { 13 14 /** 15 * @param args 16 */ 17 public static void main(String[] args) { 18 try { 19 //创建解析工厂 20 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 21 //创建解析器 22 DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); 23 //通过解析器读取配置文件,生成一个Document[org.w3c.dom]对象树 24 Document document = builder.parse("conf/bookstore.xml"); 25 26 //创建XPath对象 27 XPath xPath = XPathFactory.newInstance().newXPath(); 28 29 // 1.获取bookstore节点下book属性category值为web下的第二个title节点的文本内容 30 // bookstore -> book[@category='web'][2] -> title 31 // xpath路径:/bookstore/book[@category='web'][2]/title/text() 32 String titleXpath = "/bookstore/book[@category='web'][2]/title/text()"; 33 String titleValue = (String) xPath.evaluate(titleXpath, document, XPathConstants.STRING); 34 System.out.println(titleValue); 35 36 // 2.获取bookstore节点下book属性category值为web的titile属性为en的节点内容 37 // bookstore -> book[@category='web'] -> title[@lang='en'] 38 // xpath路径:/bookstore/book[@category='web']/title[@lang='en']/text() 39 String titleLangXpath = "/bookstore/book[@category='web']/title[@lang='en']/text()"; 40 String titleLangValue = (String) xPath.evaluate(titleLangXpath, document, XPathConstants.STRING); 41 System.out.println(titleLangValue); 42 43 // 3.获取bookstore下book属性category值为cooking的title的lang属性的值 44 // bookstore -> book[@category='cooking'] -> title ->@lang 45 // xpath路径:/bookstore/book[@category='cooking']/title/@lang 46 String titleLangAttrXpath = "/bookstore/book[@category='cooking']/title/@lang"; 47 String titleLangAttrValue = (String) xPath.evaluate(titleLangAttrXpath, document, XPathConstants.STRING); 48 System.out.println(titleLangAttrValue); 49 50 // 4.获取bookstore节点下所有book的节点集合 51 // /bookstore/book 52 NodeList bookList = (NodeList) xPath.evaluate("/bookstore/book", document, XPathConstants.NODESET); 53 //开始遍历bookList 54 for(int i = 0; i < bookList.getLength(); i++){ 55 Element bookElt = (Element) bookList.item(i); 56 String titleValue01 = (String) xPath.evaluate("title", bookElt, XPathConstants.STRING); 57 String authorValue = (String) xPath.evaluate("author", bookElt, XPathConstants.STRING); 58 String year = (String) xPath.evaluate("year", bookElt, XPathConstants.STRING); 59 String price = (String) xPath.evaluate("price", bookElt, XPathConstants.STRING); 60 System.out.println(titleValue01 + " " + authorValue + " " + year + " " + price); 61 System.out.println("---------------"); 62 } 63 } catch (Exception e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } 67 } 68 69 }