超强解析XML——简单直接的来

开发 前端
解析豆瓣返回的xml,实在是不想用DOM这个重量级的玩意。DOM这个玩意,说它强大好还是说它官僚好呢。我倾向于使用SAXP解析。但是现在面临的一个问题是,我需要根据xml节点的名字和属性值(一个或者多个)来决定当前的值是不是我想要的。

对于现在越来越轻量级,越来越讲究速度和接近用户的应用来说,xml确实有点复杂了。解析起来不仅耗内存,而且很复杂。这就好像花了几千块钱买了个MS Office,但是80%的feature都用不着,还白白的耗着CPU和内存。个人觉得,设置文件用XML其实挺好,因为设置文件一般并不太大,而且要求可读性强,还有很多乱七八糟的需求,可以利用XML的力量。昨天搞chrome的设置,发现chrome的设置文件也是使用的json,读起来也是轻松愉快。前阵子做了个程序,需要解析豆瓣API调用返回的XML。真想说一句,豆瓣你别用XML了。至少,提供个json版的API调用吧。(以上谨代表个人观点)

解析豆瓣返回的xml,实在是不想用DOM这个重量级的玩意。DOM这个玩意,说它强大好还是说它官僚好呢。我倾向于使用SAXP解析。但是现在面临的一个问题是,我需要根据xml节点的名字和属性值(一个或者多个)来决定当前的值是不是我想要的。这就麻烦一点点。***反应是考虑xpath。后来觉得不如自己做一个得了,权当是按需定制一个轻量级的xpath。

首先定义XMLSearchUnit类,这个类的实例用来描述一个需要在XML中搜索的值,值可以是xml节点的值,或者是节点的属性。

  1. package com.deepnighttwo.resourceresolver.douban.resolver.utils;  
  2.  
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.  
  6. import org.xml.sax.Attributes;  
  7.  
  8. /**  
  9.  *   
  10.  * Represent a search task. Target could be value of a node or attribute of the  
  11.  * node.  
  12.  *   
  13.  * @author mzang  
  14.  */  
  15. public class XMLSearchUnit {  
  16.  
  17.     // attribute values to be matched during search  
  18.     private Map<String, String> attributeMatchValidation = new HashMap<String, String>();  
  19.  
  20.     // if target is an attribute, then set this member to be the attribute name.  
  21.     // if it is null or empty, then means the target is node value.  
  22.     private String expectedAttr;  
  23.  
  24.     // xml path, format is: /node_name/node_name/...  
  25.     private String xmlPath;  
  26.  
  27.     public XMLSearchUnit(String xmlPath) {  
  28.         this.xmlPath = xmlPath;  
  29.     }  
  30.  
  31.     /**  
  32.      * if current node meets the search conditions or not. Meets means the path  
  33.      * is correct and the attribute value is matched.  
  34.      *   
  35.      * @param path  
  36.      * @param attributes  
  37.      * @return  
  38.      */  
  39.     public boolean match(String path, Attributes attributes) {  
  40.         if (xmlPath.equals(path) == false) {  
  41.             return false;  
  42.         }  
  43.  
  44.         for (String key : attributeMatchValidation.keySet()) {  
  45.             String exp = attributeMatchValidation.get(key);  
  46.             String compare = attributes.getValue(key);  
  47.             if (exp.equalsIgnoreCase(compare) == false) {  
  48.                 return false;  
  49.             }  
  50.         }  
  51.         return true;  
  52.     }  
  53.  
  54.     public Map<String, String> getAttributeMatchValidation() {  
  55.         return attributeMatchValidation;  
  56.     }  
  57.  
  58.     public void addAttributeValidation(String key, String value) {  
  59.         attributeMatchValidation.put(key, value);  
  60.     }  
  61.  
  62.     public String getXmlPath() {  
  63.         return xmlPath;  
  64.     }  
  65.  
  66.     public void setAttributeMatchValidation(  
  67.             Map<String, String> attributeMatchValidation) {  
  68.         this.attributeMatchValidation = attributeMatchValidation;  
  69.     }  
  70.  
  71.     public String getExpectedAttr() {  
  72.         return expectedAttr;  
  73.     }  
  74.  
  75.     /**  
  76.      * if target is node value, then set expectedAttr to null. if target is an  
  77.      * attribute value, set it to be the attribute name.  
  78.      *   
  79.      * @param expectedAttr  
  80.      */  
  81.     public void setExpectedAttr(String expectedAttr) {  
  82.         this.expectedAttr = expectedAttr;  
  83.     }  
  84.  
  85.     /**  
  86.      * hash code can be cached if all properties are not be be changed.  
  87.      */  
  88.     @Override  
  89.     public int hashCode() {  
  90.         final int prime = 31;  
  91.         int result = 1;  
  92.         result = prime 
  93.                 * result  
  94.                 + ((attributeMatchValidation == null) ? 0  
  95.                         : attributeMatchValidation.hashCode());  
  96.         result = prime * result  
  97.                 + ((expectedAttr == null) ? 0 : expectedAttr.hashCode());  
  98.         result = prime * result + ((xmlPath == null) ? 0 : xmlPath.hashCode());  
  99.         return result;  
  100.     }  
  101.  
  102.     @Override  
  103.     public boolean equals(Object obj) {  
  104.         if (this == obj)  
  105.             return true;  
  106.         if (obj == null)  
  107.             return false;  
  108.         if (getClass() != obj.getClass())  
  109.             return false;  
  110.         XMLSearchUnit other = (XMLSearchUnit) obj;  
  111.         if (attributeMatchValidation == null) {  
  112.             if (other.attributeMatchValidation != null)  
  113.                 return false;  
  114.         } else if (!attributeMatchValidation  
  115.                 .equals(other.attributeMatchValidation))  
  116.             return false;  
  117.         if (expectedAttr == null) {  
  118.             if (other.expectedAttr != null)  
  119.                 return false;  
  120.         } else if (!expectedAttr.equals(other.expectedAttr))  
  121.             return false;  
  122.         if (xmlPath == null) {  
  123.             if (other.xmlPath != null)  
  124.                 return false;  
  125.         } else if (!xmlPath.equals(other.xmlPath))  
  126.             return false;  
  127.         return true;  
  128.     }  
  129.  

这个类比较简单。就是用一个hashmap保待匹配的attribut键值对,用一个字符串表示期待的attribute name,用一个字符串表示期待的node path。

然后就是如何在SAXP里用到这个类的实例去搜索了。
 

  1. package com.deepnighttwo.resourceresolver.douban.resolver.utils;  
  2.  
  3. import java.io.InputStream;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.  
  9. import javax.xml.parsers.SAXParser;  
  10. import javax.xml.parsers.SAXParserFactory;  
  11.  
  12. import org.xml.sax.Attributes;  
  13. import org.xml.sax.InputSource;  
  14. import org.xml.sax.SAXException;  
  15. import org.xml.sax.XMLReader;  
  16. import org.xml.sax.helpers.DefaultHandler;  
  17.  
  18. /**  
  19.  *   
  20.  * SAXP parser working with XMLSearchUnit.  
  21.  *   
  22.  * @author mzang  
  23.  */  
  24.  
  25. public class DoubanSearchParser extends DefaultHandler {  
  26.  
  27.     // create and initial search units  
  28.     public static final XMLSearchUnit DETAILS_LINK_API_PATH = new XMLSearchUnit(  
  29.             "/feed/entry/id");  
  30.  
  31.     public static final XMLSearchUnit DETAILS_CONTENT_PATH = new XMLSearchUnit(  
  32.             "/entry/summary");  
  33.  
  34.     public static final XMLSearchUnit DETAILS_TITLE_PATH = new XMLSearchUnit(  
  35.             "/entry/title");  
  36.  
  37.     public static final XMLSearchUnit DETAILS_CHINESE_NAME_PATH = new XMLSearchUnit(  
  38.             "/entry/db:attribute");  
  39.  
  40.     public static final XMLSearchUnit DETAILS_RATINGE_PATH = new XMLSearchUnit(  
  41.             "/entry/gd:rating");  
  42.  
  43.     public static final XMLSearchUnit DETAILS_RATINGE_RATER_COUNT_PATH = new XMLSearchUnit(  
  44.             "/entry/gd:rating");  
  45.  
  46.     public static final XMLSearchUnit DETAILS_LINK_URL_PATH = new XMLSearchUnit(  
  47.             "/feed/entry/link");  
  48.  
  49.     static {  
  50.         DETAILS_LINK_URL_PATH.addAttributeValidation("rel", "alternate");  
  51.         DETAILS_LINK_URL_PATH.setExpectedAttr("href");  
  52.  
  53.         DETAILS_CHINESE_NAME_PATH.addAttributeValidation("lang", "zh_CN");  
  54.         DETAILS_CHINESE_NAME_PATH.addAttributeValidation("name", "aka");  
  55.  
  56.         DETAILS_RATINGE_PATH.setExpectedAttr("average");  
  57.  
  58.         DETAILS_RATINGE_RATER_COUNT_PATH.setExpectedAttr("numRaters");  
  59.  
  60.     }  
  61.  
  62.     // a map to store the XMLSearchUnit and value  
  63.     private Map<XMLSearchUnit, String> results = new HashMap<XMLSearchUnit, String>();  
  64.  
  65.     // a counter of search unit. if it is 0, then all search unit finds a match  
  66.     // value and the result of the XML will be skipped.  
  67.     private int count = 0;  
  68.  
  69.     private StringBuilder path = new StringBuilder();  
  70.  
  71.     private static final String pathSeparater = "/";  
  72.  
  73.     private XMLSearchUnit[] searchUnits;  
  74.  
  75.     List<XMLSearchUnit> foundItems = new ArrayList<XMLSearchUnit>();  
  76.  
  77.     /**  
  78.      * constructor, accept XML input stream, 0 or more search unit instances.  
  79.      *   
  80.      * @param input  
  81.      * @param expectedPath  
  82.      * @return  
  83.      */  
  84.     public Map<XMLSearchUnit, String> parseResults(InputStream input,  
  85.             XMLSearchUnit... expectedPath) {  
  86.         for (XMLSearchUnit search : expectedPath) {  
  87.             results.put(search, null);  
  88.         }  
  89.  
  90.         searchUnits = expectedPath;  
  91.  
  92.         count = expectedPath.length;  
  93.  
  94.         XMLReader xmlReader = null;  
  95.         try {  
  96.             SAXParserFactory spfactory = SAXParserFactory.newInstance();  
  97.             spfactory.setValidating(false);  
  98.             SAXParser saxParser = spfactory.newSAXParser();  
  99.             xmlReader = saxParser.getXMLReader();  
  100.             xmlReader.setContentHandler(this);  
  101.             xmlReader.parse(new InputSource(input));  
  102.         } catch (Exception e) {  
  103.             System.err.println(e);  
  104.             System.exit(1);  
  105.         }  
  106.         return results;  
  107.     }  
  108.  
  109.     private void addToPath(String addPath) {  
  110.         path.append(pathSeparater).append(addPath.toLowerCase());  
  111.     }  
  112.  
  113.     private void popPath() {  
  114.         int index = path.lastIndexOf(pathSeparater);  
  115.         // String removedPath = path.substring(index);  
  116.         path.delete(index, path.length());  
  117.     }  
  118.  
  119.     @Override  
  120.     public void startElement(String uri, String localName, String qName,  
  121.             Attributes attributes) throws SAXException {  
  122.         foundItems.clear();  
  123.         if (count == 0) {  
  124.             return;  
  125.         }  
  126.  
  127.         // update path  
  128.         addToPath(qName);  
  129.  
  130.         List<XMLSearchUnit> foundAttrItems = null;  
  131.  
  132.         // check if current node matches search units. if it is a node value  
  133.         // search, then store it in a member variable named foundItems because  
  134.         // the value of the node is known only when reaches the end of the  
  135.         // node.but for attribute search, it value is known here. So then are  
  136.         // put in a local variable list named foundAttrItems.  
  137.         for (XMLSearchUnit unit : searchUnits) {  
  138.             if (unit.match(path.toString(), attributes) == true) {  
  139.  
  140.                 if (unit.getExpectedAttr() == null) {  
  141.                     foundItems.add(unit);  
  142.                 } else {  
  143.                     if (foundAttrItems == null) {  
  144.                         foundAttrItems = new ArrayList<XMLSearchUnit>();  
  145.                     }  
  146.                     foundAttrItems.add(unit);  
  147.                 }  
  148.             }  
  149.         }  
  150.         // if no attribute match, return.  
  151.         if (foundAttrItems == null) {  
  152.             return;  
  153.         }  
  154.  
  155.         // fill search unit value using attribute value. update count.  
  156.         for (XMLSearchUnit attrUnit : foundAttrItems) {  
  157.             String attrValue = attributes.getValue(attrUnit.getExpectedAttr());  
  158.             if (results.get(attrUnit) == null) {  
  159.                 count--;  
  160.             }  
  161.             results.put(attrUnit, attrValue);  
  162.             count--;  
  163.         }  
  164.     }  
  165.  
  166.     /**  
  167.      * if current node matches, the the node value is useful, store it.  
  168.      */  
  169.     @Override  
  170.     public void characters(char[] ch, int start, int length)  
  171.             throws SAXException {  
  172.         if (count == 0) {  
  173.             return;  
  174.         }  
  175.         if (foundItems.size() == 0) {  
  176.             return;  
  177.         }  
  178.  
  179.         for (XMLSearchUnit unit : foundItems) {  
  180.             String content = new String(ch, start, length);  
  181.             if (results.get(unit) == null) {  
  182.                 count--;  
  183.             }  
  184.             results.put(unit, content);  
  185.         }  
  186.     }  
  187.  
  188.     @Override  
  189.     public void endElement(String uri, String localName, String qName)  
  190.             throws SAXException {  
  191.         foundItems.clear();  
  192.         if (count == 0) {  
  193.             return;  
  194.         }  
  195.         popPath();  
  196.     }  

原文链接:http://www.cnblogs.com/deepnighttwo/archive/2011/03/13/1982748.html

【编辑推荐】

  1. 解析PHP中的XML数据
  2. 详解用PHP读取和编写XML DOM
  3. 利用DOM解析XML文档实例
  4. XML DOM新手入门指南
  5. 构建轻量级XML DOM分析程序
责任编辑:陈贻新 来源: Mark Zang的博客
相关推荐

2009-01-04 16:58:48

LINQ to XMLLINQXML

2021-07-29 10:00:43

XMLXMLStarletLinux

2012-08-16 15:56:33

XML

2009-02-17 11:23:08

2009-07-07 14:11:07

XML来配置Servl

2011-02-22 14:10:25

PHPXML

2012-05-23 13:17:43

JavaJdomXML

2013-06-08 11:04:18

Android开发Pull解析XMLAndroid XML

2022-04-11 08:25:37

XMLSQL语句Mybatis

2009-09-09 14:40:15

C# XML解析

2011-08-04 18:39:32

iOS XML

2022-03-22 09:41:31

Java编程语言持久化

2016-11-14 19:45:39

JavaScript

2015-08-12 10:04:24

2016-02-16 10:26:58

PythonXML方式

2010-06-17 23:13:10

路由信息协议

2010-01-25 18:22:33

Android使用XM

2010-07-26 15:06:29

Perl解析XML文件

2014-12-31 14:09:23

xml解析

2010-09-28 10:03:15

DOM文档对象模型
点赞
收藏

51CTO技术栈公众号