簡體   English   中英

使用Java DOM / SAX解析dblp.xml

[英]Parsing dblp.xml with java DOM/SAX

我試圖解析Java中的dblp.xml以獲得作者姓名/標題/年等,但是由於文件很大(860MB),因此無法在完整文件上使用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來解析大型文檔,但這也

如果您的目標只是了解細節,則只需使用BufferedReader將該文件讀取為文本文件即可。 如果需要,請輸入一些正則表達式。

如果可以選擇使用mysql,則可以通過它的XML函數使它完成繁重的工作

希望這可以幫助。

不要對xml格式大驚小怪。 無論如何,它並不是非常有用。 只需將其讀取為文本文件並將行解析為字符串即可。 然后,您可以將數據導出到csv,並從那時開始以所需的方式使用它。 不幸的是,xml對於大型文檔不是很有效。 我在這里為一個研究項目做過類似的事情: http : //qualityofdata.com/2011/03/27/dblp-for-sql-server/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM