当前位置: 首页 > 面试题库 >

在Java中使用SAX解析大型XML

范翰飞
2023-03-14
问题内容

我正在尝试分析堆栈溢出数据转储,其中一个表称为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实现。

再见

更新:改进了以前的代码段。



 类似资料:
  • 问题内容: 我需要解析一个XML流。由于我只需要做一次就可以构建我的java对象,因此SAX看起来很自然。我正在扩展DefaultHandler并实现startElement,endElement和character方法,在我的类中具有保存当前读取值(在characters方法中使用)的成员。 我可以轻松完成所需的工作,但是我的代码变得相当复杂,并且我确信没有理由这样做,并且我可以做不同的事情。我

  • 问题内容: 它很好用,但是我希望它返回一个包含所有字符串的数组,而不是最后一个元素返回一个字符串。 任何想法如何做到这一点? 问题答案: 因此,你想构建一个XML解析器来解析这样的RSS feed。 现在,你可以使用两个SAX实现。你可以使用org.xml.sax或android.sax实现。在发布简短的示例后,我将解释两者的优点和缺点。 android.sax Implementation 让我

  • 问题内容: 它很好用,但是我希望它返回一个包含所有字符串的数组,而不是最后一个元素返回一个字符串。 任何想法如何做到这一点? 问题答案: 因此,你想构建一个XML解析器来解析这样的RSS feed。 现在,你可以使用两个SAX实现。你可以使用org.xml.sax或android.sax实现。在发布简短的示例后,我将解释两者的优点和缺点。 android.sax实现 让我们从实现开始。 你首先必须

  • 主要内容:Java SAX解析器 解析XML文档的示例Java SAX解析器 解析XML文档的示例 需要解析的文件input.xml 编写DefaultHandler的事件处理程序 编写核心解析处理类 输出结果为:

  • 问题内容: 我有以下问题: 我有一个XML文件(大约1GB),并且必须上下迭代(即不连续;一个接一个),以便获取所需的数据并对其进行一些操作。最初,我使用了DOM Java包,但是很显然,在解析XML文件时,JVM达到了其最大堆空间并停止了运行。 为了解决这个问题,我想到的解决方案之一是找到另一个解析器,该解析器迭代XML中的每个元素,然后将其内容存储在硬盘上的临时SQLite数据库中。因此,通过

  • 问题内容: 我正在用SAX解析XML文件,有时需要元素的内部XML。例如,对于以下XML 我需要获取元素 a 的内部XML ,这将是 最简单的方法是什么? 谢谢。 伊万 问题答案: 对于这种情况,我建议使用2个内容处理程序。第一个负责查找文档的相关部分,第二个负责处理内容。我对类似问题的答案(请参见下面的链接)演示了如何实现此方法: 使用SAX解析常见的XML元素