• JAVA之XML文件解析


    在Java、Android开发中,xml文件的解析很重要。本人使用过的xml文件解析的方式有两种,一种是DOM文档解析、还有一种是SAX。

    DOM是基于文档树结构的、SAX是基于事件驱动的,遇到标签则触发工作的。当解析器发现元素开始、元素结束、文本、文档的开始或结束等时,发送事件,程序员编写响应这些事件的代码,保存数据。优缺点很明显:DOM对于大型文件的操作不好(因为它要先读入整个文档到内存中),SAX为了解决这样问题。不用事先调入整个文档,占用资源少;SAX解析器代码比DOM解析器代码小,适于Applet,下载。缺点:不是持久的;事件过后,若没保存数据,那么数据就丢了;无状态性;从事件中只能得到文本,但不知该文本属于哪个元素;使用场合:Applet;只需XML文档的少量内容,很少回头访问;机器内存少。

    1、DOM方式解析

     1 package com.hk.xml;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 import javax.xml.parsers.DocumentBuilder;
     6 import javax.xml.parsers.DocumentBuilderFactory;
     7 import javax.xml.parsers.ParserConfigurationException;
     8 import org.w3c.dom.Document;
     9 import org.w3c.dom.NamedNodeMap;
    10 import org.w3c.dom.Node;
    11 import org.w3c.dom.NodeList;
    12 import org.xml.sax.SAXException;
    13 public class DOMParseXML {
    14 
    15     public DocumentBuilder getDocBuilder()
    16     {
    17         DocumentBuilder db = null;
    18         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    19         try 
    20         {
    21             db = dbFactory.newDocumentBuilder();
    22         } catch (ParserConfigurationException e) 
    23         {
    24             e.printStackTrace();
    25         }
    26         return db;
    27     }
    28     
    29     public void XMLParse(){
    30         DocumentBuilder db = getDocBuilder();
    31         try {
    32             Document document = db.parse(new File("books.xml"));
    33             //获取所有所有book节点
    34             NodeList bookList = document.getElementsByTagName("book");
    35             for (int i = 0; i < bookList.getLength(); i++) {
    36                 Node node = bookList.item(i);
    37                 NamedNodeMap map = node.getAttributes();
    38                 for(int j=0; j<map.getLength(); j++){
    39                     Node mNode = map.item(j);
    40                     System.out.println(mNode.getNodeName()+":"+mNode.getNodeValue());
    41                 }
    42             }
    43         } catch (SAXException e) {
    44             // TODO 自动生成的 catch 块
    45             e.printStackTrace();
    46         } catch (IOException e) {
    47             // TODO 自动生成的 catch 块
    48             e.printStackTrace();
    49         }
    50     }
    51     public static void main(String[] args) {
    52         DOMParseXML parse = new DOMParseXML();
    53         parse.XMLParse();
    54     }
    55 }

    2、SAX方式

     1 package com.hk.xml;
     2 
     3 import java.util.List;
     4 
     5 import org.xml.sax.Attributes;
     6 import org.xml.sax.SAXException;
     7 import org.xml.sax.helpers.DefaultHandler;
     8 
     9 import com.hk.entity.Mp3Info;
    10 
    11 public class Mp3ListContentHandler extends DefaultHandler {
    12 
    13     private List<Mp3Info> infos = null;
    14     public List<Mp3Info> getInfos() {
    15         return infos;
    16     }
    17 
    18     public Mp3ListContentHandler(List<Mp3Info> infos) {
    19         super();
    20         this.infos = infos;
    21     }
    22 
    23     public void setInfos(List<Mp3Info> infos) {
    24         this.infos = infos;
    25     }
    26 
    27     private Mp3Info mp3Info = null;
    28     private String tagName = null;
    29     @Override
    30     public void startDocument() throws SAXException {
    31         System.out.println("------开始解析XML文件------");
    32     }
    33 
    34     @Override
    35     public void endDocument() throws SAXException {
    36         System.out.println("------XML文件解析结束------");
    37     }
    38 
    39     @Override
    40     public void startElement(String uri, String localName, String qName,
    41             Attributes attributes) throws SAXException {
    42         this.tagName = localName;
    43         if(tagName.equals("resource")){
    44             mp3Info = new Mp3Info();
    45         }
    46     }
    47 
    48     @Override
    49     public void endElement(String uri, String localName, String qName)
    50             throws SAXException {
    51         
    52         if(qName.equals("resource")){
    53             infos.add(mp3Info);
    54         }
    55         tagName = "";  
    56     }
    57 
    58     @Override
    59     public void characters(char[] ch, int start, int length)
    60             throws SAXException {
    61         String temp = new String(ch,start,length);
    62         if(tagName.equals("id")){
    63             mp3Info.setId(temp);
    64         }else if(tagName.equals("mp3.name")){
    65             mp3Info.setMp3Name(temp);
    66         }else if(tagName.equals("mp3.size")){
    67             mp3Info.setMp3Size(temp);
    68         }else if(tagName.equals("lrc.size")){
    69             mp3Info.setLrcSize(temp);
    70         }else if(tagName.equals("lrc.name")){
    71             mp3Info.setLrcName(temp);
    72         }
    73     }
    74 }
  • 相关阅读:
    crmfuxi
    段子
    wsfenxiang
    生成器、列表推导式
    闭包、迭代器、递归
    函数的参数及返回值
    嵌套、作用域、命名空间
    定义、函数的调用
    测试样式
    进制转换
  • 原文地址:https://www.cnblogs.com/hker/p/4768394.html
Copyright © 2020-2023  润新知