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();
//开始遍历根节点
Iterator<Element> ele = root.elementIterator();
while(ele.hasNext()) {
Element stu = ele.next();//得到每个学生
//准备遍历学生里面的节点
Iterator<Element> inner = stu.elementIterator();
while(inner.hasNext()) {
//里面的标签
Element xinxi = inner.next();
//拿到该节点内容
String stringValue = xinxi.getStringValue();
System.out.println(stringValue);
}
System.out.println("==========");
}
//config/studednt.xml
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student>
<name>吴京</name>
<age>35</age>
<notes>男,演员</notes>
</student>
<student>
<name>谢楠</name>
<age>34</age>
<notes>女,主持人</notes>
</student>
<student>
<name>胡歌</name>
<age>39</age>
<notes>男,演员</notes>
</student>
<student>
<name>金泰妍</name>
<age>30</age>
<notes>女,主持人,歌手</notes>
</student>
</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类
class MyDefaultHandler extends DefaultHandler{ @Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//开始的标签
System.out.print("<"+qName+">");
} @Override
public void characters(char[] ch, int start, int length) throws SAXException {
//字符串
System.out.print(new String(ch,start,length));
} @Override
public void endElement(String uri, String localName, String qName) throws SAXException {
//结束标签---
System.out.print("</"+qName+">");
}
}
//config/person.xml
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person>
<name>张三</name>
<age>15</age>
<sex>男</sex>
<job>学生</job>
</person>
<person>
<name>李四</name>
<age>25</age>
<sex>男</sex>
<job>程序员</job>
</person>
<person>
<name>王五</name>
<age>35</age>
<sex>女</sex>
<job>老板</job>
</person>
<person>
<name>金泰妍</name>
<age>30</age>
<sex>女</sex>
<job>歌手</job>
</person>
<person>
<name>IU</name>
<age>29</age>
<sex>女</sex>
<job>演员</job>
</person>
</persons>
DOM4j和xpath解析XML获取节点元素的文本内容
用xpath要导入jaxen-1.1-beta-7.jar
public class Test3_xpath { public static void main(String[] args) throws DocumentException, ClassNotFoundException, SQLException {
//jaxen-1.1-beta-7.jar
//用XPath语法就要用到上边这个jar包
//dom4j+xpath解析 SAXReader reader=new SAXReader(); Document doc=reader.read("config/sys-config.xml"); //Element root = doc.getRootElement();
//driver-name节点元素的路径:config--》database-info--》driver-name
//driver-name节点元素的xpath路径:/config/database-info/driver-name //返回值Node 参数:String --强制类型转换
//直接获取到指定节点元素 Element eledrivername= (Element) doc.selectSingleNode("/config/database-info/driver-name");
//获取该节点元素对象的文本内容
String drivername = eledrivername.getStringValue();
System.out.println(drivername); //url节点元素的路径:config-->database-info-->url
//url节点元素的xpath路径:/config/database-info/url
//url节点元素的xpath路径:config//url
//url节点元素的xpath路径://url
Element eleurl = (Element) doc.selectSingleNode("config//url");
String url = eleurl.getStringValue();
System.out.println(url); //user节点元素的路径:config-->database-info-->user
//user节点元素的xpath路径:/config/database-info/user
//user节点元素的xpath路径:config//user
//user节点元素的xpath路径://user
Element eleuser = (Element) doc.selectSingleNode("//user");
String user = eleuser.getText();//也可获得
System.out.println(user); //password节点元素的路径:config-->database-info-->password
//password节点元素的xpath路径:/config/database-info/password
Element elepassword = (Element) doc.selectSingleNode("//password");
String password = elepassword.getStringValue();
System.out.println(password); System.out.println("-------------连接数据库-------------------"); Class.forName(drivername);
Connection con=(Connection) DriverManager.getConnection(url,user,password);
Statement stmt = con.createStatement();
String sql="select * from user";
ResultSet result = stmt.executeQuery(sql);
System.out.println(result); while(result.next()) {
String string = result.getString(1);
String string2 = result.getString(2);
System.out.println("user:"+string+",password:"+string2);
}
result.close();
stmt.close();
con.close(); }
}
//config/sys-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<database-info>
<driver-name>com.mysql.jdbc.Driver</driver-name>
<url>jdbc:mysql://localhost:3306/mydb3</url>
<user>root</user>
<password>123456</password>
</database-info>
</config>
DOM4j和xpath解析XML获取节点元素的属性对象的值
获取指定节点元素的属性对象 以及属性对象的值
port.getName()----port 属性对象的名字
port.getValue()-----8080 属性对象的值
或者直接用该节点对象.attributeValue("属性名")也能获取对应的值
public class Test4_ServerParser { public static void main(String[] args) throws DocumentException {
SAXReader saxReader=new SAXReader();
Document doc = saxReader.read("config/server.xml");
//获取connector节点元素对象的路径:server-->service-->connector
Element eleconnector = (Element) doc.selectSingleNode("server/service/connector");
//获取eleconnector的属性port
Attribute port = eleconnector.attribute("port");
//获取属性对象的值
String portvalue = port.getValue();
String portvalue2 = eleconnector.attributeValue("port");
System.out.println(portvalue);
System.out.println(portvalue2);
}
}
//config/server.xml
<?xml version="1.0" encoding="UTF-8"?>
<server>
<service>
<connector port="8080"></connector>
</service>
</server>
结果:8080
8080