自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

超強(qiáng)解析XML——簡(jiǎn)單直接的來

開發(fā) 前端
解析豆瓣返回的xml,實(shí)在是不想用DOM這個(gè)重量級(jí)的玩意。DOM這個(gè)玩意,說它強(qiáng)大好還是說它官僚好呢。我傾向于使用SAXP解析。但是現(xiàn)在面臨的一個(gè)問題是,我需要根據(jù)xml節(jié)點(diǎn)的名字和屬性值(一個(gè)或者多個(gè))來決定當(dāng)前的值是不是我想要的。

對(duì)于現(xiàn)在越來越輕量級(jí),越來越講究速度和接近用戶的應(yīng)用來說,xml確實(shí)有點(diǎn)復(fù)雜了。解析起來不僅耗內(nèi)存,而且很復(fù)雜。這就好像花了幾千塊錢買了個(gè)MS Office,但是80%的feature都用不著,還白白的耗著CPU和內(nèi)存。個(gè)人覺得,設(shè)置文件用XML其實(shí)挺好,因?yàn)樵O(shè)置文件一般并不太大,而且要求可讀性強(qiáng),還有很多亂七八糟的需求,可以利用XML的力量。昨天搞chrome的設(shè)置,發(fā)現(xiàn)chrome的設(shè)置文件也是使用的json,讀起來也是輕松愉快。前陣子做了個(gè)程序,需要解析豆瓣API調(diào)用返回的XML。真想說一句,豆瓣你別用XML了。至少,提供個(gè)json版的API調(diào)用吧。(以上謹(jǐn)代表個(gè)人觀點(diǎn))

解析豆瓣返回的xml,實(shí)在是不想用DOM這個(gè)重量級(jí)的玩意。DOM這個(gè)玩意,說它強(qiáng)大好還是說它官僚好呢。我傾向于使用SAXP解析。但是現(xiàn)在面臨的一個(gè)問題是,我需要根據(jù)xml節(jié)點(diǎn)的名字和屬性值(一個(gè)或者多個(gè))來決定當(dāng)前的值是不是我想要的。這就麻煩一點(diǎn)點(diǎn)。***反應(yīng)是考慮xpath。后來覺得不如自己做一個(gè)得了,權(quán)當(dāng)是按需定制一個(gè)輕量級(jí)的xpath。

首先定義XMLSearchUnit類,這個(gè)類的實(shí)例用來描述一個(gè)需要在XML中搜索的值,值可以是xml節(jié)點(diǎn)的值,或者是節(jié)點(diǎn)的屬性。

  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.  

這個(gè)類比較簡(jiǎn)單。就是用一個(gè)hashmap保待匹配的attribut鍵值對(duì),用一個(gè)字符串表示期待的attribute name,用一個(gè)字符串表示期待的node path。

然后就是如何在SAXP里用到這個(gè)類的實(shí)例去搜索了。
 

  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數(shù)據(jù)
  2. 詳解用PHP讀取和編寫XML DOM
  3. 利用DOM解析XML文檔實(shí)例
  4. XML DOM新手入門指南
  5. 構(gòu)建輕量級(jí)XML DOM分析程序
責(zé)任編輯:陳貽新 來源: Mark Zang的博客
相關(guān)推薦

2009-01-04 16:58:48

LINQ to XMLLINQXML

2021-07-29 10:00:43

XMLXMLStarletLinux

2012-08-16 15:56:33

XML

2011-02-22 14:10:25

PHPXML

2009-02-17 11:23:08

2009-07-07 14:11:07

XML來配置Servl

2013-06-08 11:04:18

Android開發(fā)Pull解析XMLAndroid XML

2012-05-23 13:17:43

JavaJdomXML

2009-09-09 14:40:15

C# XML解析

2022-04-11 08:25:37

XMLSQL語句Mybatis

2015-08-12 10:04:24

2022-03-22 09:41:31

Java編程語言持久化

2011-08-04 18:39:32

iOS XML

2010-06-17 23:13:10

路由信息協(xié)議

2016-11-14 19:45:39

JavaScript

2016-02-16 10:26:58

PythonXML方式

2010-09-28 10:03:15

DOM文檔對(duì)象模型

2014-12-31 14:09:23

xml解析

2009-09-09 13:57:28

C# XML解析

2010-09-09 13:55:47

XML DOM
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)