hooyantsing's Blog

第54次课程

字数统计: 2k阅读时长: 10 min
2019/09/05

源辰74班

第54次课程

09.05.2019

内容

java解析XML[蒋斌]

**1.SAX解析器 **作业讲解

SAX解析器

SAX一行一行读取

startElement:开始读取标记体<标记>    endElement:结束读取标记体</标记>    characters:读取标记体字符串

db.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<!-- 连接数据库配置文件 -->
<dbconfig>
    <db>
       <driver>oracle.jdbc.driver.OracleDriver</driver>
       <url>jdbc:oracle:thin:@localhost:1521:orcl</url>
       <username>scott</username>
       <pass>tiger</pass>
    </db>
</dbconfig>

SAXRead.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package task;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

/**
* SAX解析器
* SAX一行一行读取
* startElement:开始读取标记体<标记>    endElement:结束读取标记体</标记>    characters:读取标记体字符串
*/

public class SAXRead extends DefaultHandler {
    public static SAXRead sr;
    
    public SAXRead() {
        //创建解析器工厂
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            //通过解析器工厂创建解析器
            SAXParser parser = factory.newSAXParser();    
            //通过解析器创建读取器
            XMLReader xmlReader = parser.getXMLReader();
            //注册事件处理器
            xmlReader.setContentHandler(this);
            //设置xml文件路径
            xmlReader.parse(SAXRead.class.getClassLoader().getResource("db.xml").getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SAXRead saxRead = new SAXRead();
        DB opendb = saxRead.db;
        System.out.println(opendb);

    }
    
    public static DB newInstance() {
        if(sr==null) {
            sr = new SAXRead();
        }
        return sr.db;
    }
    
    public DB db;
    private String qName;
    //开始读取标记体<标记>
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        this.qName = qName;
        if("db".equals(qName)) {
            db = new DB();
        }
    }
    //读取标记体字符串
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if("driver".equals(qName)) {
            db.setDriver(new String(ch,start,length));
        }
        if("url".equals(qName)) {
            db.setUrl(new String(ch,start,length));
        }
        if("pass".equals(qName)) {
            db.setPass(new String(ch,start,length));
        }
        if("username".equals(qName)) {
            db.setUsername(new String(ch,start,length));
        }
        //若无qName = null,每次读取完<标记>后,再读取</标记>将其覆盖,导致输出不正常
        qName = null;
    }
    //结束读取标记体</标记>
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        // TODO 自动生成的方法存根
        super.endElement(uri, localName, qName);
    }
}

DBHelper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package task;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author Hooy
* 读取XML配置文件后,用于连接数据库
*/
public class DBHelper {
    static {
        try {
            Class.forName(SAXRead.newInstance().getDriver());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        openConnection();
    }
    public static void openConnection() {
        try {
            Connection conn = DriverManager.getConnection(SAXRead.newInstance().getUrl(),SAXRead.newInstance().getUsername(),SAXRead.newInstance().getPass());
            System.out.println(conn);
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
}

DB.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package task;
/**
*
* @author Hooy
* 封装读取到的XML配置文件信息
*/
public class DB {
    private String driver;
    private String url;
    private String username;
    private String pass;
    public String getDriver() {
       return driver;
    }
    public void setDriver(String driver) {
       this.driver = driver;
    }
    public String getUrl() {
       return url;
    }
    public void setUrl(String url) {
       this.url = url;
    }
    public String getUsername() {
       return username;
    }
    public void setUsername(String username)  {
       this.username = username;
    }
    public String getPass() {
       return pass;
    }
    public void setPass(String pass) {
       this.pass = pass;
    }
    @Override
    public String toString() {
       return "DB [driver=" + driver + ",  url=" + url + ", username=" + username + ",  pass=" + pass + "]";
    }
}

2.旧Dom解析器 W3C.DOM

Document和Element区别:

Document:对象是整个xml文档

Element:对象是通过**.getElementsByTagName(“标记名”)**已选择的结点

book.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
       <name>西游记</name>
       <author>吴承恩</author>
       <price>100</price>
    </book>
    <book>
       <name>红楼梦</name>
       <author>曹雪芹</author>
       <price>200</price>
    </book>
</books>

Dom.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package dom;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import  javax.xml.parsers.DocumentBuilderFactory;
import  javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
* @author Hooy
* Dom解析器
* 将整个xml文档信息读入内存当中,产生一个Document对象,再从这个对象中获取所需要的信息
*/
public class Dom {
    public static void main(String[] args)  throws ParserConfigurationException,  SAXException, IOException {
       //创建解析器工厂
       DocumentBuilderFactory factory =  DocumentBuilderFactory.newInstance();
       //通过解析器工厂创建dom解析器
       DocumentBuilder builder =  factory.newDocumentBuilder();
       //获取xml文件路径,生成Document对象
       Document doc =  builder.parse("book.xml");
       
       //阶梯编程
       //.getElementsByTagName 按文档顺序返回包含在文档中且具有给定标记名称的所有 Element 的  NodeList
       NodeList nlist =  doc.getElementsByTagName("name");
       //此处 [<name>西游记</name>][<name>红楼梦</name>]
       Node node = nlist.item(1);
       //此处 [<name>红楼梦</name>]
       Node firstNode =  node.getFirstChild();
       //此处 [红楼梦]
       String nodeValue =  firstNode.getNodeValue();
       //此处  红楼梦
       System.out.println(nodeValue);
       /*
       //链式编程
       String value =  doc.getElementsByTagName("name").item(1).getFirstChild().getNodeValue();
       //System.out.println(value);
       */
    }
}

SunDom.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package dom;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import  javax.xml.parsers.DocumentBuilderFactory;
import  javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class SunDom {
    public static void main(String[] args)  throws ParserConfigurationException,  SAXException, IOException {
       DocumentBuilderFactory factory =  DocumentBuilderFactory.newInstance();
       DocumentBuilder builder =  factory.newDocumentBuilder();
       Document doc =  builder.parse("book.xml");
       NodeList nodeList =  doc.getElementsByTagName("book");
       /*此处
       [<book>
           <name>西游记</name>
           <author>吴承恩</author>
           <price>100</price>
       </book>]
       [<book>
           <name>红楼梦</name>
           <author>曹雪芹</author>
           <price>200</price>
       </book>]
        */
       for(int  i=0;i<nodeList.getLength();i++) {
           Element ele =  (Element)nodeList.item(i);
           String value =  ele.getElementsByTagName("author").item(0).getFirstChild().getNodeValue();
           System.out.println(value);
       }
    }
}

**3.主流Dom **org.dom4j

通过SAXReader读取XML文档,然后Document生成document对象

首先通过.getRootElement()找到第一个标记<根标记>,然后通过它迭代器,找出其子结点。

Dom4jDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package dom;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4jDemo {
    public static void main(String[] args) {
    SAXReader reader = new SAXReader();
       try {
           //通过SAXReader读取XML文档,然后Document生成document对象
           Document document =  reader.read("book.xml");
           //根标记
           Element rootElement =  document.getRootElement();
           //获取迭代器——内含两个book标记
           Iterator iterator =  rootElement.elementIterator();
           while(iterator.hasNext()) {
               Element ele =  (Element)iterator.next();
               for(Iterator  ii=ele.elementIterator();ii.hasNext();) {
                  //[name][author][price]
                  Element cele =  (Element)ii.next();
                  //.getName() 获取标记名
                  if("name".equals(cele.getName())) {
                      //.getText() 获取字符串
                      String text =  cele.getText();
                      System.out.println(text);
                  }
               }
           }
       } catch (DocumentException e) {
           e.printStackTrace();
       }
    }
}

**4.**org.dom4j案例(天气预报XML)

Method.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package dom;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
    
public class Method {
    private static String SERVICES_HOST =  "www.webxml.com.cn";
    
    //查询天气国家
    private static String  WEATHER_COUNTRY_URL  ="http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionCountry";
    //获得中国省份、直辖市、地区和与之对应的ID
    private static String WEATHER_PRVO_URL =  "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince";
    
    //查询城市
    private static String WEATHER_CITY_URL =  "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString?theUserID=&theRegionCode=";
    
    
    //天气查询地址
    private static String WEATHER_QUERY_URL  ="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theUserID=&theCityCode=";
    public static InputStream  getsopInput(String url) {
       InputStream inputStream = null;
       try {
           URL uobj = new URL(url);
           URLConnection connection =  uobj.openConnection();
           connection.setRequestProperty("Host",  SERVICES_HOST);
           connection.connect();
           inputStream =  connection.getInputStream();
       } catch (IOException e) {
           e.printStackTrace();
       }
       return inputStream;
    }
    
    //查询所有的省份
    public static List<String> getPRVO(){
       List<String> list = new  ArrayList<String>() ;
       SAXReader saxReader = new SAXReader();
       InputStream input =  getsopInput(WEATHER_PRVO_URL);
       try {
           Document document =  saxReader.read(input);
           Element rootElement =  document.getRootElement();
           Iterator iterator =  rootElement.elementIterator();
           while(iterator.hasNext()) {
               Element ele =  (Element)iterator.next();
               list.add(ele.getText());
           }
       } catch (DocumentException e) {
           // TODO 自动生成的 catch 块
           e.printStackTrace();
       }
       return list;
    }

    //获取当前省份下的城市
    private static List<String> getCity(int  code) {
       List<String> list = new  ArrayList<String>();
       InputStream input =  getsopInput(WEATHER_CITY_URL+code);
       SAXReader saxReader = new  SAXReader();
       Document document;
       try {
           document = saxReader.read(input);
           Element rootElement =  document.getRootElement();
           Iterator iterator =  rootElement.elementIterator();
           while(iterator.hasNext()) {
           Element ele =  (Element)iterator.next();
           list.add(ele.getText());
           System.out.println(ele.getText());
           }
       } catch (DocumentException e) {
           e.printStackTrace();
       }
       return list;
    }

    public static void main(String[] args) {
       new Method().getCity(31118);
    }

}
CATALOG