簡體   English   中英

在Java中使用SAX解析大型XML

[英]parsing large XML using SAX in java

我試圖解析堆棧溢出數據轉儲,其中一個表稱為posts.xml,其中有大約1000萬個條目。 樣本XML:

<?xml version="1.0" encoding="utf-8"?>
<posts>
  <row Id="1" PostTypeId="1" AcceptedAnswerId="26" CreationDate="2010-07-07T19:06:25.043" Score="10" ViewCount="1192" Body="&lt;p&gt;Now that the Engineer update has come, there will be lots of Engineers building up everywhere.  How should this best be handled?&lt;/p&gt;&#xA;" OwnerUserId="11" LastEditorUserId="56" LastEditorDisplayName="" LastEditDate="2010-08-27T22:38:43.840" LastActivityDate="2010-08-27T22:38:43.840" Title="In Team Fortress 2, what is a good strategy to deal with lots of engineers turtling on the other team?" Tags="&lt;strategy&gt;&lt;team-fortress-2&gt;&lt;tactics&gt;" AnswerCount="5" CommentCount="7" />
  <row Id="2" PostTypeId="1" AcceptedAnswerId="184" CreationDate="2010-07-07T19:07:58.427" Score="5" ViewCount="469" Body="&lt;p&gt;I know I can create a Warp Gate and teleport to Pylons, but I have no idea how to make Warp Prisms or know if there's any other unit capable of transporting.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;I would in particular like this to built remote bases in 1v1&lt;/p&gt;&#xA;" OwnerUserId="10" LastEditorUserId="68" LastEditorDisplayName="" LastEditDate="2010-07-08T00:16:46.013" LastActivityDate="2010-07-08T00:21:13.163" Title="What protoss unit can transport others?" Tags="&lt;starcraft-2&gt;&lt;how-to&gt;&lt;protoss&gt;" AnswerCount="3" CommentCount="2" />
  <row Id="3" PostTypeId="1" AcceptedAnswerId="56" CreationDate="2010-07-07T19:09:46.317" Score="7" ViewCount="356" Body="&lt;p&gt;Steam won't let me have two instances running with the same user logged in.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;Does that mean I cannot run a dedicated server on a PC (for example, for Left 4 Dead 2) &lt;em&gt;and&lt;/em&gt; play from another machine?&lt;/p&gt;&#xA;&#xA;&lt;p&gt;Is there a way to run the dedicated server without running steam? Is there a configuration option I'm missing?&lt;/p&gt;&#xA;" OwnerUserId="14" LastActivityDate="2010-07-07T19:27:04.777" Title="How can I run a dedicated server from steam?" Tags="&lt;steam&gt;&lt;left-4-dead-2&gt;&lt;dedicated-server&gt;&lt;account&gt;" AnswerCount="1" />
  <row Id="4" PostTypeId="1" AcceptedAnswerId="14" CreationDate="2010-07-07T19:11:05.640" Score="10" ViewCount="201" Body="&lt;p&gt;When I get to the insult sword-fighting stage of The Secret of Monkey Island, do I have to learn every single insult and comeback in order to beat the Sword Master?&lt;/p&gt;&#xA;" OwnerUserId="17" LastEditorUserId="17" LastEditorDisplayName="" LastEditDate="2010-07-08T21:25:04.787" LastActivityDate="2010-07-08T21:25:04.787" Title="Do I have to learn all of the insults and comebacks to be able to advance in The Secret of Monkey Island?" Tags="&lt;monkey-island&gt;&lt;adventure&gt;" AnswerCount="3" CommentCount="2" />

我想解析此xml,但僅加載xml的某些屬性,例如ID,PostTypeId,AcceptedAnswerId和其他2個屬性。 SAX中是否有辦法只加載這些屬性? 如果有的話怎么辦? 我對SAX還是很陌生,因此一些指導會有所幫助。

否則,加載整個程序只會很慢,而且某些屬性也不會被使用,因此毫無用處。

另一個問題是,是否有可能跳到ID為X的特定行? 如果可能的話,我該怎么做?

“ StartElement” Sax事件允許處理單個XML ELement。

在Java代碼中,您必須實現此方法

public void startElement(String uri, String localName,
    String qName, Attributes attributes)
    throws SAXException {

    if("row".equals(localName)) {
        //this code is executed for every xml element "row"
        String id = attributes.getValue("id");
        String PostTypeId = attributes.getValue("PostTypeId");
        String AcceptedAnswerId = attributes.getValue("AcceptedAnswerId");
        //others two
        // you have your att values for an "row" element
    }

 }

對於每個元素,您都可以訪問:

  1. 命名空間URI
  2. XML QName
  3. XML LocalName
  4. 屬性圖, 您可以在此處提取兩個屬性...

有關特定細節,請參見ContentHandler實現。

再見

更新:改進了以前的代碼段。

與我已經在這里回答的方法幾乎相同。

向下滾動到org.xml.sax Implementation部分。 您只需要一個自定義處理程序。

SAX不會“加載”元素。 它會通知您的應用程序每個元素的開始和結束,並且完全由您的應用程序決定它需要注意哪些元素。

暫無
暫無

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

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