三類XML文件的解析方法

XML文件的解析

mybatis的學習當中,配置的加載傳統的是通過解析用戶配置的文件,獲取到數據庫連接與對象實體映射關係的,書中有描述到DOM\SAX\StAX 幾種方案,一方面是夯實基礎,一方面是瞭解三種方法的優缺點,今天就學習一下XML文件的解析

一、XML的解析方式

  • DOM 解析XML底層接口之一,跨平臺,跨語言
  • SAX 解析XML底層接口之一
  • Jdom/dom4J 基於底層API的封裝,Java語言,更方便便捷

二、DOM解析

DOM 解析的原理:樹形結構,依賴內存加載文件,樹在內存中持久化存儲,映射成Document對象,解析DOM樹,識別父節點、當前節點、兄弟節點和子節點,在節點間遊歷,就能夠獲取到整棵樹的數據。操作簡易,消耗大,適用於小文檔或者需要頻繁修改文檔的場景。

使用JDK自帶的DocumentBuilderFactory進行解析

//生成工廠實例
  DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
//創建builder實例
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        //讀取文件內容,加載入Document對象
        Document document = documentBuilder.parse("store.xml");
        //根據Name獲取節點
        NodeList productList = document.getElementsByTagName("product");
       for(int i =0;i<productList.getLength();i++){
           Node product = productList.item(i);
           Node firstChild = product.getFirstChild();
           Node lastChild = product.getLastChild();
           String nodeName = product.getNodeName();
           short nodeType = product.getNodeType();
           String nodeValue = product.getNodeValue();
           //獲取節點內容
           NamedNodeMap namedNodeMap = product.getAttributes();
           for(int j=0;j<namedNodeMap.getLength();j++){
               Node node = namedNodeMap.item(j);
               String name = node.getNodeName();
               String value = node.getNodeValue();
               short type = node.getNodeType();
               System.out.println("name:"+name+";value:"+value);
           }
           //獲取子節點內容
           NodeList childNodes = product.getChildNodes();
           for(int k =0 ;k<childNodes.getLength();k++){
               Node item = childNodes.item(k);
               String itemNodeName = item.getNodeName();
               short itemNodeType = item.getNodeType();
               String itemNodeValue = item.getNodeValue();
               System.out.println("itemName:"+itemNodeName+";itemValue:"+itemNodeValue);

               NodeList children = item.getChildNodes();
               for(int m =0;m<children.getLength();m++){
                   Node item1 = children.item(m);
                   System.out.println("itemName:"+item1.getNodeName()+";itemValue:"+item1.getNodeValue());
               }
           }
       }

三、SAX解析

SAX解析原理:基於事件的模型,不需要將全部文檔加載入內存,只需要用戶讀取的時候加載相應的內容片段即可,響應快,消耗小,適用於大型文檔讀取的場景。

使用JDK自帶的SAXParserFactory

  • 創建一個用於解析的Handler, 重寫父類的關於片段解析的代碼
public class SaxHandler extends DefaultHandler {
    private List<Product> products = null;
    private Product product;
    private String currentTag = null;
    private String currentValue = null;
    private String nodeName = null;

    public SaxHandler(String nodeName) {
        this.nodeName = nodeName;
    }

    public List<Product> getProducts() {
        return products;
    }


    @Override
    public void startDocument() throws SAXException {
        // 讀到一個開始標籤會觸發
        super.startDocument();

        products = new ArrayList<Product>();
    }

    @Override
    public void endDocument() throws SAXException {
        //自動生成的方法存根
        super.endDocument();
    }

    @Override
    public void startElement(String uri, String localName, String name,
                             Attributes attributes) throws SAXException {
        //文檔的開頭調用
        super.startElement(uri, localName, name, attributes);

        if (name.equals(nodeName)) {
            product = new Product();
        }
        if (attributes != null && product != null) {
            for (int i = 0; i < attributes.getLength(); i++) {
                if (attributes.getQName(i).equals("id")) {
                    product.setId(Integer.valueOf(attributes.getValue(i)));
                }
            }
        }
        currentTag = name;
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        //處理在XML文件中讀到的內容
        super.characters(ch, start, length);

        if (currentTag != null && product != null) {
            currentValue = new String(ch, start, length);
            if (!currentValue.trim().equals("") && !currentValue.trim().equals("\n")) {
                if (currentTag.equals("name")) {
                    product.setName(currentValue);
                } else if (currentTag.equals("price")) {
                    product.setPrice(Double.valueOf(currentValue));
                }else if(currentTag.equals("inventory")){
                    product.setInventory(Integer.valueOf(currentValue));
                }
            }
        }
        currentTag = null;
        currentValue = null;
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        // 結束標籤的時候調用
        super.endElement(uri, localName, name);

        if (name.equals(nodeName)) {
            products.add(product);
        }
    }

}
  • 創建一個用於接收的對象(別的數據結構也可)
public class Product {
    private Integer id;
    private String name;
    private Double price;
    private Integer inventory;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Integer getInventory() {
        return inventory;
    }

    public void setInventory(Integer inventory) {
        this.inventory = inventory;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", inventory=" + inventory +
                '}';
    }
}
  • 讀取文件,並使用Handler進行解析
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxParserFactory.newSAXParser();
        SaxHandler saxHandler = new SaxHandler("product");
        saxParser.parse(new InputSource("store.xml"), saxHandler);
        List<Product> products = saxHandler.getProducts();
        products.forEach(System.out::println);

四、JDOM/Dom4J

  • JDOM的目的是成爲Java特定文檔模型,它簡化與XML的交互並且比使用DOM實現更快

  • Dom4J,是JDOM 的一種智能分支,合併了許多超出基本 XML 文檔表示的功能

  • 以下爲Dom4j爲例

  • 添加maven依賴

       <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
  • 讀取xml文件
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File("store.xml"));
  • 處理解析邏輯
        Element rootElement = document.getRootElement();
        List<Element> elements = rootElement.elements();
        List<Product> products = new ArrayList<>();
        for(Element element : elements){
            Integer id = Integer.valueOf(element.attributeValue("id"));
            String name = element.element("name").getText();
            Double price = Double.valueOf(element.element("price").getText());
            Integer inventory = Integer.valueOf(element.elementText("inventory"));
            Product product = new Product();
            product.setId(id);
            product.setName(name);
            product.setPrice(price);
            product.setInventory(inventory);
            
            products.add(product);
        }
        products.forEach(System.out::println);

All codes are here:this page

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章