tags:

views:

511

answers:

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

+10  A: 

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.

cletus
Another open source xml parser is vtd-xml, which is considered a bit more advanced than DOM, SAX and Pull
vtd-xml-author
A: 

Here is the answer of your question http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html

And you would love to read this one http://java.sun.com/developer/Books/xmljava/ch03.pdf

Sachin Chourasiya
Please refer to SO FAQ, section 'be nice' if you like to know the reason for my downvote.
Andreas_D
I like to know the reason for the downvote
Sachin Chourasiya
@Andreas, could you please tell me what wrong with my post?
Sachin Chourasiya
I don't know how you managed to edit the post without leaving a trace - but when I first read it, it clearly started with "Dude" and was offensive to my eyes. Now it"s fine with me and I'll remove my downvote now.
Andreas_D
@Sachin: he already mentioned to refer the SO FAQ, section 'be nice'.
BalusC
.. and because your edit is not visible, I can't revert the downvote. I'll remove it, when the post is edited officially (and will remove these comments aswell)
Andreas_D
@Andreas: the edits are not visible if done within the same minute.
BalusC
@BalusC There is a grace period of **5 minutes** after you make a post, not 1.
Pascal Thivent
Guys you must be happy , I have edited my post and the reason behind edit is also written.
Sachin Chourasiya
bit harsh downvoting for 'Dude'?
Paul McKenzie
@Andreas,I dont know why you downvoted me even if I have removed the word Dude which sounds ofensive and you cant believe it I am happily accepting your downvote because I am here to get knowledge and share knowledge.
Sachin Chourasiya
+1  A: 

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

BalusC
A: 

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.

CodeSeavers
A: 

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

Andreas_D
+2  A: 

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.

EJB
A: 

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

William
+1  A: 

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: http://stackoverflow.com/questions/530064/fastest-xml-parser-for-small-simple-documents-in-java

Nathan Feger
A: 

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.

Paul McKenzie
A: 

You might want to use JAXB

Stroboskop
A: 

I really like JIBX (http://jibx.sourceforge.net/), thanks to stackoverflow.com :)

Trick
A: 

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

vtd-xml-author
+1  A: 

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

Madhan
A: 

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/

Chris