筆頭。。
          實(shí)踐啟示
          posts - 14,comments - 3,trackbacks - 0
          問(wèn)題描述:比如從xml中讀取單個(gè)數(shù)據(jù)信息,文件數(shù)據(jù)信息為幾k左右。
          解決方案:方案一.從xml文件中讀取單個(gè)信息,每次讀取都重新打開(kāi)文件
                    方案二.將文件信息放到內(nèi)存中,每次通過(guò)文件信息句柄查找
                    方案三.從內(nèi)存中讀取,將文件存儲(chǔ)在hashmap中,每次通過(guò)hashmap映射
          技術(shù):使用dom4j、xpath
          example1: 通過(guò)IATA查找對(duì)應(yīng)的ICAO(IATA,ICAO參考注)
              airlines.xml
              存儲(chǔ)了airline的IATA和ICAO,root是<airlines>,root的子元素為<element>,<element>的屬性為IATA,ICAO
             
          <?xml version="1.0" encoding="UTF-8"?>
          <airlines>
            
          <element IATA="AL" ICAO="TXC"/>
            
          <element IATA="AY" ICAO="FIN"/>
            .
            .
          </airlines>
              AirlinesXml.java
              方法parse、getDocment解析xml文件,比較簡(jiǎn)單,不解釋了
          private static Document getDocument() throws DocumentException
              {
                  
          if(doc==null){
                  doc 
          = parse(new File(fileUrl));
                  }
                  
          return doc;
              }
              
          private static Document parse(File file) throws DocumentException {
                  SAXReader saxReader 
          = new SAXReader();
                  Document doc 
          = saxReader.read(file);
                  
          return doc;
              }
             方法getICAO(),striata,通過(guò)xpath直接查找對(duì)應(yīng)iata的icao ,第一調(diào)用該方法時(shí)讀取文件,保留doc,以后從doc中查找對(duì)應(yīng)信息
             
          /**
               * 如果沒(méi)有則返回null
               * 
          @param str 當(dāng)前的strIATA
               * 
          @return String icao
               
          */
              
          public static String getICAO(String strIATA)
              {
                  String tmp
          =null;
                  String xpathStr
          ="//element[@IATA='"+str+"']";
                  
          try {
                      Document doc
          =getDocument();
                      Node node
          =(Node) doc.selectSingleNode(xpathStr);
                      
          if(node!=null)
                      {
                          tmp
          =node.valueOf("@ICAO");
                      }
                  } 
          catch (DocumentException e) {
                      
          // TODO Auto-generated catch block
                      logger.error("沒(méi)有該文件,文件url為"+fileUrl,e);
                  }
                  
                  
          return tmp;
              }
              方法:getIcaoFromMap()與前一方法的不同在于第一次xpath信息遍歷所有信息存儲(chǔ)在hashmap中,以后通過(guò)hashmap查找
          public static String getICAOFromMap(String iataStr)
              {
                  
          if(airlineMap==null){
                  String xpath
          ="//element[@IATA]";
                  airlineMap
          =new HashMap<String, String>();
                  
          try{
                      Document doc
          =getDocument();
                      List list
          =doc.selectNodes(xpath);
                      
          for(Iterator iter=list.iterator();iter.hasNext();)
                      {
                          Node node
          =(Node)iter.next();
                          airlineMap.put(node.valueOf(
          "@IATA"), node.valueOf("@ICAO"));
                      }
                  }
          catch (DocumentException e) {
                          
          // TODO Auto-generated catch block
                          logger.error("沒(méi)有該文件,文件url為"+fileUrl,e);
                      }
                  
                  
                  }
                  
          return airlineMap.get(iataStr);
              }
            main函數(shù)
          public static void main(String[] args){
                  
          long time1=System.currentTimeMillis();
                  System.out.println(getICAO(
          "OV"));
                  
          long time2=System.currentTimeMillis();
                  System.out.println(
          "getICAO:第一次"+(time2-time1)+" "+(time2-time1)/1000);
                  System.out.println(getICAO(
          "CA"));
                  
          long time3=System.currentTimeMillis();
                  System.out.println(
          "getICAO:第二次"+(time3-time2)+" "+(time3-time2)/1000);
                  System.out.println(getICAOFromMap(
          "CA"));
                  
          long time4=System.currentTimeMillis();
                  System.out.println(
          "getICAOMap:第一次"+(time4-time3)+" "+(time4-time3)/1000);
                  System.out.println(getICAOFromMap(
          "OV"));
                  
          long time5=System.currentTimeMillis();
                  System.out.println(
          "getICAOMap:第二次"+(time5-time4)+" "+(time5-time4)/1000);
              }
          運(yùn)行結(jié)果為:
          NAN
          getICAO:第一次672 0
          CCA
          getICAO:第二次47 0
          CCA
          getICAOMap:第一次125 0
          NAN
          getICAOMap:第二次0 0

          可見(jiàn)xpath中查找單個(gè)數(shù)據(jù)的時(shí)間比遍歷為map后再在內(nèi)存的hashmap中慢
          xpath是樹(shù)結(jié)構(gòu)查找,所以時(shí)間為log(n)級(jí)別,所以較慢,建議當(dāng)文件不太大時(shí)可以考慮存儲(chǔ)在本地進(jìn)行存儲(chǔ)
          注:IATA、ICAO為航空公司的二字碼、三字碼
             源碼下載:http://www.aygfsteel.com/Files/onedaylover/perfomance_dom4j.rar



          posted on 2008-01-14 14:11 如果有一天de 閱讀(1862) 評(píng)論(0)  編輯  收藏 所屬分類: dom4j、xml與java

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宜阳县| 彝良县| 衡阳县| 广饶县| 湘潭县| 卢龙县| 昂仁县| 五河县| 通渭县| 柳河县| 永兴县| 富民县| 囊谦县| 偃师市| 南平市| 陆川县| 黄龙县| 吴忠市| 龙川县| 凌云县| 武平县| 永修县| 桐庐县| 无极县| 昂仁县| 安丘市| 徐汇区| 客服| 清涧县| 郓城县| 乐都县| 淄博市| 洛浦县| 丰镇市| 大理市| 新源县| 宁安市| 恩施市| 黑水县| 开平市| 防城港市|