How can I parse XML using Java?

2019-01-18 16:59发布

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

标签: java xml parsing
12条回答
SAY GOODBYE
2楼-- · 2019-01-18 17:49

There are two basic approaches to parsing XML.

  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.

查看更多
小情绪 Triste *
3楼-- · 2019-01-18 17:50

I've written a very simple API for precisely this reason. 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. It's just a single Java file that you can use as a library in your code. Hope that helps.

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

查看更多
SAY GOODBYE
4楼-- · 2019-01-18 17:51

If you only need to get XML snippets into/out of an object graph then you might consider XStream which is a simple lightweight marshalling/unmarshalling library.

查看更多
The star\"
5楼-- · 2019-01-18 17:53

You can consider Xpath (it falls in the "tree based approach" as listed by cletus in this topic). It's the most handy/easy approach if all you want is just getting the values out of the xml document.

Here are some useful Xpath tutorials:
To learn the syntax: http://www.w3schools.com/Xpath/
To learn how to use in Java: http://www.ibm.com/developerworks/library/x-javaxpathapi.html

查看更多
何必那么认真
6楼-- · 2019-01-18 17:54

You might want to use JAXB

查看更多
家丑人穷心不美
7楼-- · 2019-01-18 17:57

I really like(and its just my opinion) the SAX approach when you know well the structure of your files.Here is a link that may help you SAX2 Tutorial

查看更多
登录 后发表回答