广告
返回顶部
首页 > 资讯 > 前端开发 > node.js >XMLSearchUnit类怎么定义
  • 685
分享到

XMLSearchUnit类怎么定义

2024-04-02 19:04:59 685人浏览 安东尼
摘要

这篇“XMLSearchUnit类怎么定义”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“X

这篇“XMLSearchUnit类怎么定义”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“XMLSearchUnit类怎么定义”文章吧。

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

package com.deepnighttwo.resourceresolver.douban.resolver.utils;   import java.util.HashMap;  import java.util.Map;   import org.xml.sax.Attributes;     public class XMLSearchUnit {       // attribute values to be matched during search      private Map<String, String> attributeMatchValidation = new HashMap<String, String>();       // if target is an attribute, then set this member to be the attribute name.      // if it is null or empty, then means the target is node value.      private String expectedAttr;       // xml path, fORMat is: /node_name/node_name/...      private String xmlPath;       public XMLSearchUnit(String xmlPath) {          this.xmlPath = xmlPath;      }             public boolean match(String path, Attributes attributes) {          if (xmlPath.equals(path) == false) {              return false;          }           for (String key : attributeMatchValidation.keySet()) {              String exp = attributeMatchValidation.get(key);              String compare = attributes.getValue(key);              if (exp.equalsIgnoreCase(compare) == false) {                  return false;              }          }          return true;      }       public Map<String, String> getAttributeMatchValidation() {          return attributeMatchValidation;      }       public void addAttributeValidation(String key, String value) {          attributeMatchValidation.put(key, value);      }       public String getXmlPath() {          return xmlPath;      }       public void setAttributeMatchValidation(              Map<String, String> attributeMatchValidation) {          this.attributeMatchValidation = attributeMatchValidation;      }       public String getExpectedAttr() {          return expectedAttr;      }             public void setExpectedAttr(String expectedAttr) {          this.expectedAttr = expectedAttr;      }             @Override      public int hashCode() {          final int prime = 31;          int result = 1;          result = prime                 * result                  + ((attributeMatchValidation == null) ? 0                          : attributeMatchValidation.hashCode());          result = prime * result                  + ((expectedAttr == null) ? 0 : expectedAttr.hashCode());          result = prime * result + ((xmlPath == null) ? 0 : xmlPath.hashCode());          return result;      }       @Override      public boolean equals(Object obj) {          if (this == obj)              return true;          if (obj == null)              return false;          if (getClass() != obj.getClass())              return false;          XMLSearchUnit other = (XMLSearchUnit) obj;          if (attributeMatchValidation == null) {              if (other.attributeMatchValidation != null)                  return false;          } else if (!attributeMatchValidation                  .equals(other.attributeMatchValidation))              return false;          if (expectedAttr == null) {              if (other.expectedAttr != null)                  return false;          } else if (!expectedAttr.equals(other.expectedAttr))              return false;          if (xmlPath == null) {              if (other.xmlPath != null)                  return false;          } else if (!xmlPath.equals(other.xmlPath))              return false;          return true;      }   }

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

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

package com.deepnighttwo.resourceresolver.douban.resolver.utils;   import java.io.InputStream;  import java.util.ArrayList;  import java.util.HashMap;  import java.util.List;  import java.util.Map;   import javax.xml.parsers.SAXParser;  import javax.xml.parsers.SAXParserFactory;   import org.xml.sax.Attributes;  import org.xml.sax.InputSource;  import org.xml.sax.SAXException;  import org.xml.sax.XMLReader;  import org.xml.sax.helpers.DefaultHandler;      public class DoubanSearchParser extends DefaultHandler {       // create and initial search units      public static final XMLSearchUnit DETaiLS_LINK_api_PATH = new XMLSearchUnit(              "/feed/entry/id");       public static final XMLSearchUnit DETAILS_CONTENT_PATH = new XMLSearchUnit(              "/entry/summary");       public static final XMLSearchUnit DETAILS_TITLE_PATH = new XMLSearchUnit(              "/entry/title");       public static final XMLSearchUnit DETAILS_CHINESE_NAME_PATH = new XMLSearchUnit(              "/entry/db:attribute");       public static final XMLSearchUnit DETAILS_RATINGE_PATH = new XMLSearchUnit(              "/entry/gd:rating");       public static final XMLSearchUnit DETAILS_RATINGE_RATER_COUNT_PATH = new XMLSearchUnit(              "/entry/gd:rating");       public static final XMLSearchUnit DETAILS_LINK_URL_PATH = new XMLSearchUnit(              "/feed/entry/link");       static {          DETAILS_LINK_URL_PATH.addAttributeValidation("rel", "alternate");          DETAILS_LINK_URL_PATH.setExpectedAttr("href");           DETAILS_CHINESE_NAME_PATH.addAttributeValidation("lang", "zh_CN");          DETAILS_CHINESE_NAME_PATH.addAttributeValidation("name", "aka");           DETAILS_RATINGE_PATH.setExpectedAttr("average");           DETAILS_RATINGE_RATER_COUNT_PATH.setExpectedAttr("numRaters");       }       // a map to store the XMLSearchUnit and value      private Map<XMLSearchUnit, String> results = new HashMap<XMLSearchUnit, String>();       // a counter of search unit. if it is 0, then all search unit finds a match      // value and the result of the XML will be skipped.      private int count = 0;       private StringBuilder path = new StringBuilder();       private static final String pathSeparater = "/";       private XMLSearchUnit[] searchUnits;       List<XMLSearchUnit> foundItems = new ArrayList<XMLSearchUnit>();             public Map<XMLSearchUnit, String> parseResults(InputStream input,              XMLSearchUnit... expectedPath) {          for (XMLSearchUnit search : expectedPath) {              results.put(search, null);          }           searchUnits = expectedPath;           count = expectedPath.length;           XMLReader xmlReader = null;          try {              SAXParserFactory spfactory = SAXParserFactory.newInstance();              spfactory.setValidating(false);              SAXParser saxParser = spfactory.newSAXParser();              xmlReader = saxParser.getXMLReader();              xmlReader.setContentHandler(this);              xmlReader.parse(new InputSource(input));          } catch (Exception e) {              System.err.println(e);              System.exit(1);          }          return results;      }       private void addToPath(String addPath) {          path.append(pathSeparater).append(addPath.toLowerCase());      }       private void popPath() {          int index = path.lastIndexOf(pathSeparater);          // String removedPath = path.substring(index);          path.delete(index, path.length());      }       @Override      public void startElement(String uri, String localName, String qName,              Attributes attributes) throws SAXException {          foundItems.clear();          if (count == 0) {              return;          }           // update path          addToPath(qName);           List<XMLSearchUnit> foundAttrItems = null;           // check if current node matches search units. if it is a node value          // search, then store it in a member variable named foundItems because          // the value of the node is known only when reaches the end of the          // node.but for attribute search, it value is known here. So then are          // put in a local variable list named foundAttrItems.          for (XMLSearchUnit unit : searchUnits) {              if (unit.match(path.toString(), attributes) == true) {                   if (unit.getExpectedAttr() == null) {                      foundItems.add(unit);                  } else {                      if (foundAttrItems == null) {                          foundAttrItems = new ArrayList<XMLSearchUnit>();                      }                      foundAttrItems.add(unit);                  }              }          }          // if no attribute match, return.          if (foundAttrItems == null) {              return;          }           // fill search unit value using attribute value. update count.          for (XMLSearchUnit attrUnit : foundAttrItems) {              String attrValue = attributes.getValue(attrUnit.getExpectedAttr());              if (results.get(attrUnit) == null) {                  count--;              }              results.put(attrUnit, attrValue);              count--;          }      }             @Override      public void characters(char[] ch, int start, int length)              throws SAXException {          if (count == 0) {              return;          }          if (foundItems.size() == 0) {              return;          }           for (XMLSearchUnit unit : foundItems) {              String content = new String(ch, start, length);              if (results.get(unit) == null) {                  count--;              }              results.put(unit, content);          }      }       @Override      public void endElement(String uri, String localName, String qName)              throws SAXException {          foundItems.clear();          if (count == 0) {              return;          }          popPath();      }  }

以上就是关于“XMLSearchUnit类怎么定义”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网node.js频道。

--结束END--

本文标题: XMLSearchUnit类怎么定义

本文链接: https://www.lsjlt.com/news/88273.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • XMLSearchUnit类怎么定义
    这篇“XMLSearchUnit类怎么定义”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“X...
    99+
    2022-10-19
  • ES6怎么定义类
    这篇文章主要介绍“ES6怎么定义类”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“ES6怎么定义类”文章能帮助大家解决问题。 在ES6中,...
    99+
    2022-10-19
  • C#类怎么定义
    本篇内容介绍了“C#类怎么定义”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!C# 类(Class)当你定义一个类时,你定义了一个数...
    99+
    2023-06-17
  • php中怎么定义类
    本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑php类的定义方法在 PHP 中,可以使用 class 关键字加类名的方式定义一个类,然后用大括号{}将在类体中定义类的属性和方法包裹起来,类的语法格式如下:[修饰类的...
    99+
    2015-12-08
    php 定义类
  • VB.NET中怎么定义类
    VB.NET中怎么定义类,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。一个类要实现某个接口,实现接口的语句不能跟VB.NET类定义同行Public Class&n...
    99+
    2023-06-17
  • python中类怎么定义
    这篇文章主要介绍了python中类怎么定义,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。类的定义# class是定义类的关键字,ClassName为类的名称clas...
    99+
    2023-06-25
  • python类方法怎么定义
    本篇内容介绍了“python类方法怎么定义”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!定义类方法的定义需要借助于装饰器。在定义类方法时,需...
    99+
    2023-06-30
  • C++中怎么自定义类
    这篇文章将为大家详细讲解有关C++中怎么自定义类,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。/////////////TestClass.h/////////////////// &nbs...
    99+
    2023-06-17
  • Java怎么定义Long类型
    今天小编给大家分享一下Java怎么定义Long类型的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。Java定义Long数据类型...
    99+
    2023-07-02
  • java怎么定义线程类
    在Java中,可以通过以下步骤来定义一个线程类:1. 创建一个类,并让它继承自Thread类。2. 在类中重写Thread类的run...
    99+
    2023-08-08
    java
  • java怎么用static定义类
    在 Java 中,使用 `static` 关键字可以定义静态成员和静态方法。要使用 `static` 定义一个类,需要在类的声明中将...
    99+
    2023-08-24
    java static
  • VB.NET中怎么自定义类型
    VB.NET中怎么自定义类型,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。VB.NET自定义类型在VB.NET中称为“structure”(结构),包含有一个或多个不同种类的数...
    99+
    2023-06-17
  • c++类怎么定义与使用
    在C++中,可以使用class关键字来定义一个类。类定义包括类的成员变量和成员函数。以下是一个简单的C++类的定义和使用的示例:``...
    99+
    2023-09-26
    c++
  • Javascript中怎么定义一个类
    这期内容当中小编将会给大家带来有关Javascript中怎么定义一个类,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。其实Javascript中没有类这个定义,但是有类这个...
    99+
    2022-10-19
  • html5怎么定义文档类型
    这篇文章主要讲解了“html5怎么定义文档类型”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“html5怎么定义文档类型”吧! 在...
    99+
    2022-10-19
  • python类的继承怎么定义
    这篇文章主要介绍“python类的继承怎么定义”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python类的继承怎么定义”文章能帮助大家解决问题。说明基于一个现有的类来获得它所有的能力,并以此来扩展...
    99+
    2023-06-30
  • python怎么定义类和对象
    这篇“python怎么定义类和对象”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“python怎么定义类和对象”文章吧。定义简...
    99+
    2023-06-29
  • Java的组合类怎么定义
    这篇文章主要讲解了“Java的组合类怎么定义”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java的组合类怎么定义”吧!我们可以在一个新类的定义中使用其他对象。这就是组合(compositi...
    99+
    2023-06-02
  • java类加载器怎么自定义
    要自定义Java类加载器,可以继承ClassLoader类,并重写它的findClass()方法。下面是一个简单的示例:```jav...
    99+
    2023-09-14
    java
  • 怎么在Java中定义泛型类
    本篇文章为大家展示了怎么在Java中定义泛型类,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Java的特点有哪些Java的特点有哪些1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论...
    99+
    2023-06-14
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作