XML
XML简介
XML被设计用来传输和存储数据。
XML是可扩展标记语言。
解析XML的几种方式
DOM,DOM4j------先把XML文件读取到内存中,然后再解析
SAX----------边读边解析,适用于大文件的
DOM4j解析
导入dom4j jar包----dom4j-1.6.1.jar
SAXReader,Document,Element,Attribute等都是dom4j包下的
1.创建一个xml解析器对象
SAXReader reader=new SAXReader();
2.通过解析器的read方法将配置文件读取内存中,生成一个Document【org.dom4j】树
Document doc=(Document) reader.read("config/student.xml");
3.获取根节点
Element root = doc.getRootElement();
//开始遍历根节点
1 Iterator<Element> ele = root.elementIterator(); 2 while(ele.hasNext()) { 3 Element stu = ele.next();//得到每个学生 4 //准备遍历学生里面的节点 5 Iterator<Element> inner = stu.elementIterator(); 6 while(inner.hasNext()) { 7 //里面的标签 8 Element xinxi = inner.next(); 9 //拿到该节点内容 10 String stringValue = xinxi.getStringValue(); 11 System.out.println(stringValue); 12 } 13 System.out.println("=========="); 14 }
//config/studednt.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <students> 3 <student> 4 <name>吴京</name> 5 <age>35</age> 6 <notes>男,演员</notes> 7 </student> 8 <student> 9 <name>谢楠</name> 10 <age>34</age> 11 <notes>女,主持人</notes> 12 </student> 13 <student> 14 <name>胡歌</name> 15 <age>39</age> 16 <notes>男,演员</notes> 17 </student> 18 <student> 19 <name>金泰妍</name> 20 <age>30</age> 21 <notes>女,主持人,歌手</notes> 22 </student> 23 </students>
SAX解析
SAX方式:事件驱动,边读边写,解析大的xml文件。是JDK中自带的
优点:无需将整个文档加载到内存中,所以内存消耗少,适合解析特别大的xml文件。
SAX解析四步:
1.创建解析工厂:通过newInstance()方法获取
//它的构造方法受保护-----javax.xml.parsers包下的类
SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
2.通过解析工厂创建解析器
SAXParser saxParser=saxParserFactory.newSAXParser();
-------------------------------------------------------
解析器里的方法parser(解析的意思)方法,传入;两个参数:xml路径,事件处理器。
saxParser.parser("person.xml",new MyDefaultHandler());
DefaultHandler---默认解析器---里面有方法
characters---接收元素内的字符数据通知
startElement---开始
endElement---结束
//MyDefaultHandler类继承了DefaultHandler类
1 class MyDefaultHandler extends DefaultHandler{ 2 3 @Override 4 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 5 //开始的标签 6 System.out.print("<"+qName+">"); 7 } 8 9 @Override 10 public void characters(char[] ch, int start, int length) throws SAXException { 11 //字符串 12 System.out.print(new String(ch,start,length)); 13 } 14 15 @Override 16 public void endElement(String uri, String localName, String qName) throws SAXException { 17 //结束标签--- 18 System.out.print("</"+qName+">"); 19 } 20 }
//config/person.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <persons> 3 <person> 4 <name>张三</name> 5 <age>15</age> 6 <sex>男</sex> 7 <job>学生</job> 8 </person> 9 <person> 10 <name>李四</name> 11 <age>25</age> 12 <sex>男</sex> 13 <job>程序员</job> 14 </person> 15 <person> 16 <name>王五</name> 17 <age>35</age> 18 <sex>女</sex> 19 <job>老板</job> 20 </person> 21 <person> 22 <name>金泰妍</name> 23 <age>30</age> 24 <sex>女</sex> 25 <job>歌手</job> 26 </person> 27 <person> 28 <name>IU</name> 29 <age>29</age> 30 <sex>女</sex> 31 <job>演员</job> 32 </person> 33 </persons>
DOM4j和xpath解析XML获取节点元素的文本内容
用xpath要导入jaxen-1.1-beta-7.jar
1 public class Test3_xpath { 2 3 public static void main(String[] args) throws DocumentException, ClassNotFoundException, SQLException { 4 //jaxen-1.1-beta-7.jar 5 //用XPath语法就要用到上边这个jar包 6 //dom4j+xpath解析 7 8 9 SAXReader reader=new SAXReader(); 10 11 Document doc=reader.read("config/sys-config.xml"); 12 13 //Element root = doc.getRootElement(); 14 //driver-name节点元素的路径:config--》database-info--》driver-name 15 //driver-name节点元素的xpath路径:/config/database-info/driver-name 16 17 //返回值Node 参数:String --强制类型转换 18 //直接获取到指定节点元素19 Element eledrivername= (Element) doc.selectSingleNode("/config/database-info/driver-name"); 20 //获取该节点元素对象的文本内容 21 String drivername = eledrivername.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 eleurl = (Element) doc.selectSingleNode("config//url"); 29 String url = eleurl.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 eleuser = (Element) doc.selectSingleNode("//user"); 37 String user = eleuser.getText();//也可获得 38 System.out.println(user); 39 40 //password节点元素的路径:config-->database-info-->password 41 //password节点元素的xpath路径:/config/database-info/password 42 Element elepassword = (Element) doc.selectSingleNode("//password"); 43 String password = elepassword.getStringValue(); 44 System.out.println(password); 45 46 System.out.println("-------------连接数据库-------------------"); 47 48 Class.forName(drivername); 49 Connection con=(Connection) DriverManager.getConnection(url,user,password); 50 Statement stmt = con.createStatement(); 51 String sql="select * from user"; 52 ResultSet result = stmt.executeQuery(sql); 53 System.out.println(result); 54 55 while(result.next()) { 56 String string = result.getString(1); 57 String string2 = result.getString(2); 58 System.out.println("user:"+string+",password:"+string2); 59 } 60 result.close(); 61 stmt.close(); 62 con.close(); 63 64 } 65 }
//config/sys-config.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <config> 3 <database-info> 4 <driver-name>com.mysql.jdbc.Driver</driver-name> 5 <url>jdbc:mysql://localhost:3306/mydb3</url> 6 <user>root</user> 7 <password>123456</password> 8 </database-info> 9 </config>
DOM4j和xpath解析XML获取节点元素的属性对象的值
获取指定节点元素的属性对象 以及属性对象的值
port.getName()----port 属性对象的名字
port.getValue()-----8080 属性对象的值
或者直接用该节点对象.attributeValue("属性名")也能获取对应的值
1 public class Test4_ServerParser { 2 3 public static void main(String[] args) throws DocumentException { 4 SAXReader saxReader=new SAXReader(); 5 Document doc = saxReader.read("config/server.xml"); 6 //获取connector节点元素对象的路径:server-->service-->connector 7 Element eleconnector = (Element) doc.selectSingleNode("server/service/connector"); 8 //获取eleconnector的属性port 9 Attribute port = eleconnector.attribute("port"); 10 //获取属性对象的值 11 String portvalue = port.getValue(); 12 String portvalue2 = eleconnector.attributeValue("port"); 13 System.out.println(portvalue); 14 System.out.println(portvalue2); 15 } 16 }
//config/server.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <server> 3 <service> 4 <connector port="8080"></connector> 5 </service> 6 </server>
结果:8080
8080