日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

大容量XML文件解析輔助--xml批量分解 - OO - Java - JavaEye論壇

 jijo 2008-09-02





在項目里面遇到了一些被解析的xml文件超過30M 或 60M 以上的情況, 現(xiàn)在已經(jīng)不好去說為什么不在一開始產(chǎn)生xml的情況下就把xml 做小點,但是遇到這個問題后,我只能解決問題了,解決問題同時害怕重復(fù)發(fā)明輪子,我也去看了下現(xiàn)有的xml 解析東西,jdom 的SAXBuilder和 dom4j 的SAXReader都是把XML文件一次讀入,xml文件過來 會報溢出的異常 但即使SAXParser是可以批量讀入解析,但它也是一次解析完,假設(shè)XML文件中有一萬條數(shù)據(jù),解析后就必須在內(nèi)存中放這么多的對象 個人覺得這樣有些不靈活,就自己做了個小東西來切分 但前提是這個xml文件得有文件頭 <?xml version="1.0" encoding="GBK"?> encoding必須跟文件編碼格式一致 ,不然解析的時候會出亂碼。

個人水平有限,但很希望得到大家的指正,希望大家不吝嗇手中的磚頭
Java代碼 復(fù)制代碼
  1. package searchRing.ring.util.xmlBufferTool;   
  2.   
  3. import java.io.*;   
  4. import java.util.regex.Pattern;   
  5. import java.util.regex.Matcher;   
  6.   
  7.   
  8. public class XMLBufferTool {   
  9.     private static final int defaultLineCount = 10;   
  10.     private static final int defaultMaxOutputSize = 50;   
  11.   
  12.     private static final Pattern elementPattern = Pattern.compile("<[a-zA-Z]+>");   
  13.     private static final Pattern charSetPattern = Pattern.compile("<[?][[0-9a-zA-Z]|[\\s]|[=]|[\"]|[.]|[-]]+[?]>");   
  14.   
  15.     private StringBuffer xmlContentBuffer;   
  16.   
  17.   
  18.     /* just used to store and output the data divided */  
  19.     XMLOutputBuffer xmlOutput;   
  20.   
  21.     private String charSetTitle = "";   
  22.   
  23.     private String rootElemetMark = "";   
  24.   
  25.     private String childElementMark = "";   
  26.   
  27.   
  28.     InputStreamReader bufferedReader;   
  29.     InputStream fileInputStream;   
  30.   
  31.   
  32.     public XMLBufferTool(String xmlFilePath) {   
  33.   
  34.         this.xmlContentBuffer = new StringBuffer();   
  35.   
  36.         try {   
  37.   
  38.             this.fileInputStream = new FileInputStream(xmlFilePath);   
  39. //             bufferedReader = new InputStreamReader(fileInputStream, "UTF-8");   
  40.             String charSet = getCharSet(xmlFilePath);   
  41.             if (charSet != null)   
  42.                 bufferedReader = new InputStreamReader(fileInputStream, charSet);   
  43.             else  
  44.                 bufferedReader = new InputStreamReader(fileInputStream);   
  45.         } catch (FileNotFoundException fe) {   
  46.             fe.printStackTrace();   
  47.         } catch (UnsupportedEncodingException uee) {   
  48.             uee.printStackTrace();   
  49.         } catch (IOException ioe) {   
  50.             ioe.printStackTrace();   
  51.         }   
  52.   
  53.   
  54.         try {   
  55.             preparePaser();   
  56.         } catch (IOException ie) {   
  57.             ie.printStackTrace();   
  58.         }   
  59.     }   
  60.   
  61.   
  62.     public String getCharSetTitle() {   
  63.         return charSetTitle;   
  64.     }   
  65.   
  66.     public String getRootElemetMark() {   
  67.         return rootElemetMark;   
  68.     }   
  69.   
  70.     private String getCharSet(String filePath) throws IOException {   
  71.         char temp[] = new char[512];   
  72.         FileInputStream tempInput = new FileInputStream(filePath);   
  73.         InputStreamReader tempReader = new InputStreamReader(tempInput);   
  74.   
  75.         int i = tempReader.read(temp);   
  76.   
  77.         tempReader.close();   
  78.         tempInput.close();   
  79.         if (i < 0)   
  80.             return null;   
  81.   
  82.         String tempStr = new String(temp);   
  83.         Matcher m = charSetPattern.matcher(tempStr);   
  84.         if (m.find()) {   
  85.             String charSetStr = tempStr.substring(m.start(), m.end());   
  86.             Pattern tempP = Pattern.compile("[\"][[0-9a-zA-Z]|[-]]+[\"]");   
  87.             Matcher tempM = tempP.matcher(charSetStr);   
  88.             if (tempM.find()) {   
  89.                 String charSet = charSetStr.substring(tempM.start(), tempM.end());   
  90.                 return charSet.substring(1, charSet.length() - 1);   
  91.             }   
  92.         }   
  93.   
  94.         return null;   
  95.     }   
  96.   
  97.   
  98.     private void preparePaser() throws IOException {   
  99.         readSomeLine(defaultLineCount);   
  100.         Matcher m = charSetPattern.matcher(xmlContentBuffer);   
  101.         if (m.find()) {   
  102.             this.charSetTitle = this.xmlContentBuffer.substring(m.start(), m.end());   
  103.             this.xmlContentBuffer.delete(0, m.end());   
  104.         }   
  105.   
  106.         m = elementPattern.matcher(xmlContentBuffer);   
  107.         if (m.find()) {   
  108.             this.rootElemetMark = this.xmlContentBuffer.substring(m.start(), m.end());   
  109.             this.xmlContentBuffer.delete(0, m.end());   
  110.         }   
  111.   
  112.         m = elementPattern.matcher(xmlContentBuffer);   
  113.         if (m.find()) {   
  114.             this.childElementMark = this.xmlContentBuffer.substring(m.start(), m.end());   
  115.         }   
  116.         this.xmlOutput = new XMLOutputBuffer(this.childElementMark);   
  117.   
  118.         parserBuffer();   
  119.     }   
  120.   
  121.   
  122.     private int readSomeLine(int lineCount) throws IOException {   
  123.   
  124.         char buffer[] = new char[1024];   
  125.         int i = 0;   
  126.         int index = 0;   
  127.         /* be careful of the sequence of the boolean caculation */  
  128.         while (i++ < lineCount && (index = this.bufferedReader.read(buffer)) > 0) {   
  129.             xmlContentBuffer.append(buffer, 0, index);   
  130.         }   
  131.   
  132.         return index;   
  133.   
  134.     }   
  135.   
  136.   
  137.     private void parserBuffer() {   
  138.   
  139.         int lastIndex = this.xmlContentBuffer.lastIndexOf(this.childElementMark);   
  140.   
  141.         if (lastIndex > 0) {   
  142.             this.xmlOutput.append(this.xmlContentBuffer.substring(0, lastIndex));   
  143.             this.xmlContentBuffer.delete(0, lastIndex);   
  144.         }   
  145.     }   
  146.   
  147.     public StringBuffer popDividedDataAfterParser() throws IOException {   
  148.   
  149.         while (this.xmlOutput.getItemCount() < defaultMaxOutputSize) {   
  150.             int i = readSomeLine(defaultLineCount);   
  151.             parserBuffer();   
  152.             if (i < 0)   
  153.                 break;   
  154.         }   
  155.   
  156.         if (this.xmlOutput.getItemCount() == 0)   
  157.             return null;   
  158.   
  159.         StringBuffer returnSB = this.xmlOutput.getXmlOutput();   
  160.         this.xmlOutput.clearBuffer();   
  161.         return returnSB.insert(0this.rootElemetMark).append(this.rootElemetMark.replaceFirst("<""</"));   
  162.   
  163.     }   
  164.   
  165.   
  166.     public static void main(String args[]) throws Exception {   
  167.         String str = "F:/ringInfoXML/ringTime.xml";   
  168.   
  169.         XMLBufferTool xmlb = new XMLBufferTool(str);   
  170.   
  171.         StringBuffer s = xmlb.popDividedDataAfterParser();   
  172.         int i = 0;   
  173.         Matcher m = Pattern.compile("<ring>").matcher(s);   
  174.         while (m.find())   
  175.             i++;   
  176.   
  177.         System.out.println(i);   
  178.         System.out.println(s);   
  179.   
  180.   
  181.     }   
  182.   
  183.     private static class XMLOutputBuffer {   
  184.         private StringBuffer xmlOutput;   
  185.         private int itemCount;   
  186.   
  187.         private Pattern markPattern;   
  188.   
  189.         XMLOutputBuffer(String markStr) {   
  190.             this.markPattern = Pattern.compile(markStr);   
  191.             xmlOutput = new StringBuffer();   
  192.             itemCount = 0;   
  193.         }   
  194.   
  195.         public void append(String str) {   
  196.             if (str == null || "".equals(str))   
  197.                 return;   
  198.             this.xmlOutput.append(str);   
  199.             Matcher m = this.markPattern.matcher(str);   
  200.             while (m.find())   
  201.                 this.itemCount++;   
  202.         }   
  203.   
  204.         public void clearBuffer() {   
  205.             xmlOutput = new StringBuffer();   
  206.             this.itemCount = 0;   
  207.         }   
  208.   
  209.         public StringBuffer getXmlOutput() {   
  210.             return xmlOutput;   
  211.         }   
  212.   
  213.         public int getItemCount() {   
  214.             return itemCount;   
  215.         }   
  216.     }   
  217.   
  218.   
  219. }  

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多