<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    注銷

    注銷

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      112 隨筆 :: 7 文章 :: 18 評論 :: 0 Trackbacks

    ?

    目前在java平臺上,要解析xml文檔,即使只有"<abc></abc>"這樣的一個(gè)標(biāo)簽,在生成document對象時(shí),也至少要花費(fèi)300ms左右,這樣一次交互至少要在600ms左右,加上其它處理,一次通訊要1000ms以上,使得soap協(xié)議在java平臺上根本不能進(jìn)行實(shí)際應(yīng)用.

    其它這并不是SOAP協(xié)議的問題,著關(guān)鍵在于對XML文檔的解析.基于這個(gè)原因,筆者實(shí)現(xiàn)了用正則表達(dá)式來解析XML文檔的一些API,利用它來在替換中移動(dòng)的大多數(shù)SOAP的接口,效率提高了10倍左右.

    package org.axman.xml.regex;

    import java.util.regex.*;
    import java.util.*;

    /**
    ?*
    ?* <p>Title: Document</p>
    ?*
    ?* <p>Description: 用正則表達(dá)式解析xml,目的是為了提高性能.</p>
    ?*
    ?* <p>Copyright: Copyright (c) 2005</p>
    ?*
    ?* <p>Company: org.axman</p>
    ?*
    ?* @author :Axman
    ?* @version 1.0
    ?*/
    public class Document {
    ? private String xmlString;

    ? /**
    ?? * 傳入xml的字符串內(nèi)容,對于InputStream,Reader對象請轉(zhuǎn)換為String對象后傳入構(gòu)造方法.
    ?? * @param xmlString String
    ?? * @throws IllegalArgumentException
    ?? */
    ? public Document(String xmlString) throws IllegalArgumentException{
    ??? if(xmlString == null || xmlString.length() == 0)
    ????? throw new IllegalArgumentException("Input string orrer!");
    ??? this.xmlString = xmlString;
    ? }


    ? /**
    ?? * 在文檔中搜索指定的元素,返回符合條件的元素?cái)?shù)組.
    ?? * @param tagName String
    ?? * @return String[]
    ?? */
    ? public String[] getElementsByTag(String tagName){
    ??? Pattern p = Pattern.compile("<"+tagName+"[^>]*?((>.*?</"+tagName+">)|(/>))");
    ??? Matcher m = p.matcher(this.xmlString);
    ??? ArrayList<String> al = new ArrayList<String>();
    ??? while(m.find())
    ????? al.add(m.group());
    ??? String[] arr = al.toArray(new String[al.size()]);
    ??? al.clear();
    ??? return arr;
    ? }


    ? /**
    ?? * 用xpath模式提取元素,以#為分隔符
    ?? * 如 ROOT#PARENT#CHILD表示提取ROOT元素下的PARENT元素下的CHILD元素
    ?? * @param singlePath String
    ?? * @return String
    ?? */
    ? public String getElementBySinglePath(String singlePath){
    ??? String[] path = singlePath.split("#");
    ??? String lastTag = path[path.length-1];
    ??? String tmp = "(<"+lastTag+"[^>]*?((>.*?</"+lastTag+">)|(/>)))";
    ??????????????????????????????????????? //最后一個(gè)元素,可能是<x>v</x>形式或<x/>形式
    ??? for(int i=path.length-2;i >=0;i--){
    ????? lastTag = path[i];
    ????? tmp = "<"+lastTag+">.*"+tmp + ".*</"+lastTag+">";
    ??? }
    ??? Pattern p = Pattern.compile(tmp);
    ??? Matcher m = p.matcher(this.xmlString);
    ??? if(m.find()){
    ????? return m.group(1);
    ??? }
    ??? return "";
    ? }

    ? /**
    ?? * 用xpath模式提取元素從多重元素中獲取指批定元素,以#為分隔符
    ?? * 元素后無索引序號則默認(rèn)為0: ROOT#PARENT[2]#CHILD[1]
    ?? * @param singlePath String
    ?? * @return String
    ?? */
    ? public String getElementByMultiPath(String singlePath){
    ??? try{
    ????? String[] path = singlePath.split("#");
    ????? String input = this.xmlString;
    ????? String[] ele = null;
    ????? for (int i = 0; i < path.length; i++) {
    ??????? Pattern p = Pattern.compile("(\\w+)(\\[(\\d+)\\])?");
    ??????? Matcher m = p.matcher(path[i]);
    ??????? if (m.find()) {
    ????????? String tagName = m.group(1);
    ????????? System.out.println(input + "----" + tagName);
    ????????? int index = (m.group(3) == null) ? 0 :
    ????????????? new Integer(m.group(3)).intValue();
    ????????? ele = getElementsByTag(input, tagName);
    ????????? input = ele[index];
    ??????? }
    ????? }
    ????? return input;
    ??? }catch(Exception e){
    ????? return null;
    ??? }
    ? }

    ? /**
    ?? * 在給定的元素中搜索指定的元素,返回符合條件的元素?cái)?shù)組.對于不同級別的同名元素限制作用,即可以
    ?? * 搜索元素A中的子元素C.而對于元素B中子元素C則過慮,通過多級限定可以準(zhǔn)確定位.
    ?? * @param parentElementString String
    ?? * @param tagName String
    ?? * @return String[]
    ?? */
    ? public static String[] getElementsByTag(String parentElementString,String tagName){
    ??? Pattern p = Pattern.compile("<"+tagName+"[^>]*?((>.*?</"+tagName+">)|(/>))");
    ??? Matcher m = p.matcher(parentElementString);
    ??? ArrayList<String> al = new ArrayList<String>();
    ??? while(m.find())
    ????? al.add(m.group());
    ??? String[] arr = al.toArray(new String[al.size()]);
    ??? al.clear();
    ??? return arr;
    ? }

    ? /**
    ?? * 從指定的父元素中根據(jù)xpath模式獲取子元素,singlePath以#為分隔符
    ?? * 如 ROOT#PARENT#CHILD表示提取ROOT元素下的PARENT元素下的CHILD元素
    ?? * @param parentElementString String
    ?? * @param singlePath String
    ?? * @return String
    ?? */
    ? public static String getElementBySinglePath(String parentElementString,String singlePath){
    ??? String[] path = singlePath.split("#");
    ??? String lastTag = path[path.length-1];
    ??? String tmp = "(<"+lastTag+"[^>]*?((>.*?</"+lastTag+">)|(/>)))";
    ??????????????????????????????????????? //最后一個(gè)元素,可能是<x>v</x>形式或<x/>形式
    ??? for(int i=path.length-2;i >=0;i--){
    ????? lastTag = path[i];
    ????? tmp = "<"+lastTag+">.*"+tmp + ".*</"+lastTag+">";
    ??? }
    ??? Pattern p = Pattern.compile(tmp);
    ??? Matcher m = p.matcher(parentElementString);
    ??? if(m.find()){
    ????? return m.group(1);
    ??? }
    ??? return "";
    ? }

    ? /**
    ?? * 用xpath模式提取元素從指定的多重元素中獲取指批定元素,以#為分隔符
    ?? * @param parentElementString String
    ?? * @param singlePath String
    ?? * @return String
    ?? */
    ? public static String getElementByMultiPath(String parentElementString,String singlePath){
    ??? try{
    ????? String[] path = singlePath.split("#");
    ????? String input = parentElementString;
    ????? String[] ele = null;
    ????? for (int i = 0; i < path.length; i++) {
    ??????? Pattern p = Pattern.compile("(\\w+)(\\[(\\d+)\\])?");
    ??????? Matcher m = p.matcher(path[i]);
    ??????? if (m.find()) {
    ????????? String tagName = m.group(1);
    ????????? int index = (m.group(3) == null) ? 0 :
    ????????????? new Integer(m.group(3)).intValue();
    ????????? ele = getElementsByTag(input, tagName);
    ????????? input = ele[index];
    ??????? }
    ????? }
    ????? return input;
    ??? }catch(Exception e){
    ????? return null;
    ??? }
    ? }


    ? /**
    ?? * 在給定的元素中獲取所有屬性的集合.該元素應(yīng)該從getElementsByTag方法中獲取
    ?? * @param elementString String
    ?? * @return HashMap
    ?? */
    ? public HashMap<String,String> getAttributes(String elementString){
    ??? HashMap hm = new HashMap<String,String>();
    ??? Pattern p = Pattern.compile("<[^>]+>");
    ??? Matcher m = p.matcher(elementString);
    ??? String tmp = m.find()?m.group():"";
    ??? p = Pattern.compile("(\\w+)\\s*=\\s*\"([^\"]+)\"");
    ??? m = p.matcher(tmp);
    ??? while(m.find()){
    ????? hm.put(m.group(1).trim(),m.group(2).trim());
    ??? }
    ??? return hm;
    ? }


    ? /**
    ?? * 在給定的元素中獲取指定屬性的值.該元素應(yīng)該從getElementsByTag方法中獲取
    ?? * @param elementString String
    ?? * @param attributeName String
    ?? * @return String
    ?? */
    ? public static String getAttribute(String elementString,String attributeName){
    ??? HashMap hm = new HashMap<String,String>();
    ??? Pattern p = Pattern.compile("<[^>]+>");
    ??? Matcher m = p.matcher(elementString);
    ??? String tmp = m.find()?m.group():"";
    ??? p = Pattern.compile("(\\w+)\\s*=\\s*\"([^\"]+)\"");
    ??? m = p.matcher(tmp);
    ??? while(m.find()){
    ????? if(m.group(1).trim().equals(attributeName))
    ??????? return m.group(2).trim();
    ??? }
    ??? return "";
    ? }


    ? /**
    ?? * 獲取指定元素的文本內(nèi)容
    ?? * @param elementString String
    ?? * @return String
    ?? */
    ? public static String getElementText(String elementString){
    ??? Pattern p = Pattern.compile(">([^<>]*)<");
    ??? Matcher m = p.matcher(elementString);
    ??? if(m.find()){
    ????? return m.group(1);
    ??? }
    ??? return "";
    ? }

    ? public static void main(String[] args){
    ??? new Document("<ROOT>sss <PARENT>sss <CHILD>aaaa</CHILD>ss </PARENT>sss </ROOT>").getElementByMultiPath("ROOT[0]#PARENT#CHILD");
    ??? //System.out.println(child);
    ? }

    }

    posted on 2006-11-15 17:41 注銷..... 閱讀(1278) 評論(0)  編輯  收藏 所屬分類: .net摘要
    主站蜘蛛池模板: 亚洲国产成人精品无码一区二区| 少妇亚洲免费精品| 老汉色老汉首页a亚洲| 在线观看免费黄色网址| 国产精品亚洲w码日韩中文| 深夜久久AAAAA级毛片免费看| 免费看国产曰批40分钟| 黄网站色视频免费观看45分钟| 日本免费一区二区三区最新| 狠狠入ady亚洲精品| 国产免费人人看大香伊| 一级全免费视频播放| 亚洲日韩欧洲无码av夜夜摸| 日韩电影免费在线观看| 亚洲福利视频一区二区三区| 亚洲人成网站免费播放| 国产亚洲视频在线观看| 中文字幕亚洲无线码a| 青青青国产手机频在线免费观看| 亚洲色大成网站www永久| 97无码免费人妻超级碰碰夜夜| 亚洲国产成人无码AV在线| 国产高清免费的视频| 成av免费大片黄在线观看| 久久精品国产亚洲av麻豆| 久久经典免费视频| 国产亚洲综合久久| 亚洲AV一宅男色影视| 无码人妻一区二区三区免费 | 18未年禁止免费观看| 欧洲 亚洲 国产图片综合| 亚洲女同成人AⅤ人片在线观看| 99精品免费视频| 国产精品亚洲片夜色在线| 亚洲国产精品丝袜在线观看| 野花香高清在线观看视频播放免费| 亚洲精品中文字幕无乱码| 日本19禁啪啪无遮挡免费动图| 你懂的免费在线观看| 亚洲日本久久久午夜精品| 国产亚洲视频在线播放|