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条回答
男人必须洒脱
2楼-- · 2019-01-18 17:36

As described by cletus, you have to choose between the event based approach or the DOM tree you´ll have to traverse. 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.

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.

查看更多
你好瞎i
3楼-- · 2019-01-18 17:37

Hard to tell the actual problem. For simplicity's sake I'd build a DOM tree and read the data from that structure.

A typical class for TL1Command then could look like:

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. (was that the issue?)

查看更多
疯言疯语
4楼-- · 2019-01-18 17:39

You might also want to know vtd-xml, another open source XML parsing/indexing lib ...

查看更多
Emotional °昔
5楼-- · 2019-01-18 17:40

use either DOM(Document Object Model)parser or SAX(Simple API for XML)parser to parse your xml document. First create an xml document. (file with .xml extension) download the parser from the link http://archive.apache.org/dist/xml/xerces-j/ and parse your document

查看更多
戒情不戒烟
6楼-- · 2019-01-18 17:47

Are you simply looking for a library that will allow you to read the xml into an object graph?

see here for a list of many parsers: http://java-source.net/open-source/xml-parsers

A very commonly used library:

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

Another SO question: Fastest XML parser for small, simple documents in Java

查看更多
叼着烟拽天下
7楼-- · 2019-01-18 17:48

Simplest way would be to load the document as a DOM Document

Then get what you need using 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.

查看更多
登录 后发表回答