简体   繁体   English

如何使用Java解析XML?

[英]How can I parse XML using Java?

<?xml version="1.0" ?> 
<input>
    <sys>
      <protocol>TL1</protocol> 
      <ipAddress>10.05.2.3</ipAddress>
      <port>2001</port>
      <prompt>agent</prompt>       
       <TL1Command>
           <type>get</type>
           <command_code>...........</command_code>
           <staging_block>      
                <tid>...........</tid>
                <aid>...........</aid>
                <ctag>..........</ctag>
                <gen_block>.....</gen_block>
           </staging_block>
           <payload_block>
                <data_block>.......</data_block>
           </payload_block>
    </TL1Command>
    </sys>
    <sys>
      <protocol>TL1</protocol> 
      <ipAddress>10.5.2.98</ipAddress>
      <port>2001</port>
      <prompt>agent</prompt>       
       <TL1Command>
           <type>get</type>
           <command_code>...........</command_code>
           <staging_block>      
                <tid>...........</tid>
                <aid>...........</aid>
                <ctag>..........</ctag>
                <gen_block>.....</gen_block>
           </staging_block>
           <payload_block>
                <data_block>.......</data_block>
                <data_block>.......</data_block>
                <data_block>.......</data_block>
           </payload_block>
    </TL1Command>
    </sys>
</input>

I want to know how to parse this XML using Java. 我想知道如何使用Java解析这个XML。 Such that I can use that data as it is in the same given way for my program. 这样我可以使用该数据,因为它对我的程序使用相同的方式。 I know of how to parse it but the problem is for each command there might be different number of data blocks. 我知道如何解析它,但问题是每个命令可能有不同数量的数据块。 So after parsing I need to use respective datablocks for respective commands. 因此在解析之后,我需要为各自的命令使用相应的数据块。 I mean for first command while retriving I should get only one data block value and for 2nd command 3 data blocks and so on. 我的意思是在重新启动时第一个命令我应该只获得一个数据块值,而第二个命令则获得3个数据块,依此类推。 Please let me know any sample code for solving this issue. 请让我知道解决此问题的任何示例代码。

There are two basic approaches to parsing XML. 解析XML有两种基本方法。

  1. A cursor (StAX) or event (SAX) based approach. 基于游标(StAX)或事件(SAX)的方法。 Much more lightweight but often more verbose. 更轻巧但通常更冗长。 This is particularly good when you only want to grab out small parts or the processing is easy; 当你只想抓取小零件或加工很容易时,这一点特别好; and
  2. Tree-based approaches (DOM). 基于树的方法(DOM)。 Typically DOM parsers are built on top of SAX/StAX parsers anyway. 通常,DOM解析器无论如何都是建立在SAX / StAX解析器之上的。 They require more processing and typically require you to have an entire document in memory before you can do anything (whereas SAX/StAX can be much more efficient and quicker to respond). 他们需要更多的处理,通常需要你有在内存中的整个文档之前,你可以做任何事情(而SAX / StAX的可以更加有效和更快响应)。 But this is useful for "random" access to document parts. 但这对于“随机”访问文档部分很有用。

Pick whichever is most appropriate for your circumstances. 选择最适合您情况的选项。

Simplest way would be to load the document as a DOM Document 最简单的方法是将文档作为DOM Document加载

Then get what you need using XPath 然后使用XPath获得所需内容

Document document =  DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);

NodeList nodeList = XPathAPI.selectNodeList(document, "/sys");
for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    System.out.println(node.getTextContent());
}

Check out an xpath tutorial here . 在这里查看xpath教程。

You can consider Xpath (it falls in the "tree based approach" as listed by cletus in this topic). 您可以考虑使用Xpath(它属于本主题中由cletus列出的“基于树的方法”)。 It's the most handy/easy approach if all you want is just getting the values out of the xml document. 如果你想要的只是从xml文档中获取 ,这是最方便/最简单的方法。

Here are some useful Xpath tutorials: 这里有一些有用的Xpath教程:
To learn the syntax: http://www.w3schools.com/Xpath/ 要学习语法: http//www.w3schools.com/Xpath/
To learn how to use in Java: http://www.ibm.com/developerworks/library/x-javaxpathapi.html 要学习如何在Java中使用: http//www.ibm.com/developerworks/library/x-javaxpathapi.html

Are you simply looking for a library that will allow you to read the xml into an object graph? 您是否只是在寻找一个允许您将xml读入对象图形的库?

see here for a list of many parsers: http://java-source.net/open-source/xml-parsers 在这里查看许多解析器的列表: http//java-source.net/open-source/xml-parsers

A very commonly used library: 一个非常常用的库:

dom4j: http://www.dom4j.org/ dom4j: http//www.dom4j.org/

Another SO question: Fastest XML parser for small, simple documents in Java 另一个问题: 最快的XML解析器,用于Java中的小型简单文档

use either DOM(Document Object Model)parser or SAX(Simple API for XML)parser to parse your xml document. 使用DOM(文档对象模型)解析器或SAX(Simple API for XML)解析器来解析您的xml文档。 First create an xml document. 首先创建一个xml文档。 (file with .xml extension) download the parser from the link http://archive.apache.org/dist/xml/xerces-j/ and parse your document (扩展名为.xml的文件)从http://archive.apache.org/dist/xml/xerces-j/链接下载解析器并解析文档

As described by cletus, you have to choose between the event based approach or the DOM tree you´ll have to traverse. 如cletus所述,您必须在基于事件的方法或您必须遍历的DOM树之间进行选择。 Consider the event based scenario as some sort of state machine where you, as you enter the element "payload_block", you set a corresponding property and as long as it is set and the events from "data_block" come in, you read them as long as the close event from playload_block comes. 将基于事件的场景视为某种状态机,当您输入元素“payload_block”时,您设置相应的属性,只要设置它并且“data_block”中的事件进入,您就会将它们读取为long来自playload_block的关闭事件来了。

When you traverse the tree, you´ll read the children of "payload_block" and expect a list that you can iterate over and collect your data. 当您遍历树时,您将读取“payload_block”的子项并期望一个可以迭代并收集数据的列表。

Hard to tell the actual problem. 很难说出实际问题。 For simplicity's sake I'd build a DOM tree and read the data from that structure. 为简单起见,我将构建一个DOM树并从该结构中读取数据。

A typical class for TL1Command then could look like: TL1Command的典型类可能如下所示:

class TL1Command {
  String type;
  String commandLine;
  StagingBlock stagingBlock;
  List<DataBlock> dataBlocks;
}

This structure is flexible enough to handle different numbers of data blocks in each TL1Command. 该结构足够灵活,可以处理每个TL1Command中不同数量的数据块。 (was that the issue?) (是那个问题吗?)

当你非常了解你的文件结构时,我非常喜欢(而且只是我的观点)SAX方法。这里有一个链接,可以帮助你SAX2教程

如果您只需要将XML片段放入/取出对象图,那么您可以考虑使用XStream这是一个简单的轻量级编组/解组库。

您可能想要使用JAXB

您可能还想了解vtd-xml ,另一个开源XML解析/索引库...

I've written a very simple API for precisely this reason. 正是出于这个原因,我写了一个非常简单的API。 It uses the DOM parser underneath, but exposes a very simple and easy-to-use API that allows you to get to the XML data really easily. 它使用下面的DOM解析器,但是公开了一个非常简单易用的API,它允许您非常轻松地获取XML数据。 It's just a single Java file that you can use as a library in your code. 它只是一个Java文件,您可以在代码中用作库。 Hope that helps. 希望有所帮助。

http://argonrain.wordpress.com/2009/10/27/000/ http://argonrain.wordpress.com/2009/10/27/000/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM