2011-08-07 104 views
1

我試圖解析在java中dblp.xml獲得作者姓名/標題/年等,但由於該文件是巨大的(860MB),我不能使用DOM/SAX完整的文件。解析dblp.xml與Java DOM/SAX

所以我將文件分割成100MB左右各多個小文件。

現在每個文件都包含各種(千)節點是這樣的:

<dblp> 
<inproceedings mdate="2011-06-23" key="conf/aime/BianchiD95"> 
<author>Nadia Bianchi</author> 
<author>Claudia Diamantini</author> 
<title>Integration of Neural Networks and Rule Based Systems in the Interpretation of Liver  Biopsy Images.</title> 
<pages>367-378</pages> 
<year>1995</year> 
<crossref>conf/aime/1995</crossref> 
<booktitle>AIME</booktitle> 
<url>db/conf/aime/aime1995.html#BianchiD95</url> 
<ee>http://dx.doi.org/10.1007/3-540-60025-6_152</ee> 
</inproceedings> 
</dblp> 

100MB應在DOM可讀,我假設,但之後的代碼將停止大約45K線。這裏是我使用的java代碼:

@SuppressWarnings({"unchecked", "null"}) 
public List<dblpModel> readConfigDOM(String configFile) { 
    List<dblpModel> items = new ArrayList<dblpModel>(); 
    List<String> strList = null; 
    dblpModel item = null; 

    try { 

     File fXmlFile = new File(configFile); 
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     Document doc = dBuilder.parse(fXmlFile); 
     doc.getDocumentElement().normalize(); 

     NodeList nList = doc.getElementsByTagName("incollection"); 

     for (int temp = 0; temp < nList.getLength(); temp++) { 
      item = new dblpModel(); 
      strList = new ArrayList<String>(); 
      Node nNode = nList.item(temp); 
      if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

       Element eElement = (Element) nNode; 

       strList = getTagValueString("title", eElement); 
       System.out.println(strList.get(0).toString()); 

       strList = getTagValueString("author", eElement); 
       System.out.println("Author : " + strList.size()); 
       for(String s: strList) { 
        System.out.println(s); 

       } 
      } 
      items.add(item); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return items; 
} 


private static String getTagValueString(String sTag, Element eElement) { 
    String temp = ""; 
    StringBuffer concatTestSb = new StringBuffer(); 
    List<String> strList = new ArrayList<String>(); 
    int len = eElement.getElementsByTagName(sTag).getLength(); 

    try { 

     for (int i = 0; i < len; i++) { 
      NodeList nl = eElement.getElementsByTagName(sTag).item(i).getChildNodes(); 
      if (nl.getLength() > 1) { 
       for (int j = 0; j < nl.getLength(); j++) { 
        concatTestSb.append(nl.item(j).getTextContent()); 
       } 
      } else { 
       temp = nl.item(0).getNodeValue(); 
       concatTestSb.append(temp); 
       if (len > 1) { 
        concatTestSb.append("*"); 
       } 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return concatTestSb.toString(); 
} 

任何幫助嗎?我一直在使用STAX的API來解析大的文檔也試過,但也

+1

如果你說什麼「的代碼將停止」意味着你將得到更好的答案。 'readConfigDOM()'返回,還是掛起?如果它掛起,它掛在哪一行(你可以在調試器下運行和/或獲得線程轉儲)。 – parsifal

+2

順便說一句,SAX將有一個大的文件也沒有問題。 – parsifal

回答

0

如果你的目標是剛剛得到的信息出去,只是用一個BufferedReader讀取文件爲文本文件。如果你想要的話,扔入一些正則表達式。

如果使用MySQL是一個選項,你可以得到它做繁重通過它的XML Functions

希望這有助於。

0

不要大驚小怪太多的XML格式。無論如何,這並不是非常有用。只要將其作爲文本文件讀取並將這些行解析爲字符串即可。然後,您可以將數據導出到csv,並按照您希望的方式使用它。 不幸的是,對於大型文檔,xml效率不高。我也做了類似這裏的一個研究項目的內容: http://qualityofdata.com/2011/03/27/dblp-for-sql-server/