如何使用Java解析XML

如何使用Java解析XML

本文介绍了如何使用Java解析XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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>

我想知道如何使用Java解析这个XML。这样我可以使用该数据,因为它对我的程序使用相同的方式。我知道如何解析它,但问题是每个命令可能有不同数量的数据块。因此在解析之后我需要为各自的命令使用相应的数据块。我的意思是在重新启动时第一个命令我应该只获得一个数据块值,而第二个命令则获得3个数据块,依此类推。请让我知道解决此问题的任何示例代码。

I want to know how to parse this XML using Java. 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. Please let me know any sample code for solving this issue.

推荐答案

解析XML有两种基本方法。

There are two basic approaches to parsing XML.


  1. 基于游标(StAX)或事件(SAX)的方法。更轻巧但通常更冗长。当你只想抓住小零件或加工很容易时,这一点特别好;和

  2. 基于树的方法(DOM)。通常,DOM解析器无论如何都是建立在SAX / StAX解析器之上的。它们需要更多处理,并且通常要求您在执行任何操作之前将整个文档放在内存中(而SAX / StAX可以 更高效,更快速地响应)。但这对于随机访问文档部分非常有用。

  1. A cursor (StAX) or event (SAX) based approach. 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). Typically DOM parsers are built on top of SAX/StAX parsers anyway. 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). But this is useful for "random" access to document parts.

选择最适合您情况的那个。

Pick whichever is most appropriate for your circumstances.

这篇关于如何使用Java解析XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:24