aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to parse XML using Dom parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to parse XML using Dom parser" Watch "How to parse XML using Dom parser" New topic
Author

How to parse XML using Dom parser

Amarhzb Salk
Greenhorn

Joined: Feb 28, 2012
Posts: 9
Dear All,

I've two bean class one is Item and another one i ItemUpload. I'm getting one XML something like below::

<?xml version="1.0" encoding="UTF-8"?>
<FSRootNode Sessionid="" Id="" Numfiles="2">
<Items>
<Item Itemno="1" ItemType="Folder" name="FolderTest" Actualpath="" totalfilecount="4" ParentFolderID="0"/>
<File Filename="M2.TIF" path="F:\FolderTest\M2.TIF" Actualpath="\FolderTest" Id="1" Filesize="592758" Operation="1" Description="" uplaodparams="" updateparams=""/>
<File Filename="M3.TIF" path="F:\FolderTest\M3.TIF" Actualpath="\FolderTest" Id="1" Filesize="674748" Operation="1" Description="" uplaodparams="" updateparams=""/>
<SubFolder Itemno="1" ItemType="Folder" name="New folder" Actualpath="\FolderTest">
<File Filename="MApplet.java" path="F:\FolderTest\New folder\MApplet.java" Actualpath="\FolderTest\New folder" Id="1" Filesize="41703" Operation="1" Description="" uplaodparams="" updateparams=""/>
<File Filename="MApplication.java" path="F:\FolderTest\New folder\MApplication.java" Actualpath="\FolderTest\New folder" Id="1" Filesize="34476" Operation="1" Description="" uplaodparams="" dateparams=""/>
</SubFolder>
<Item Itemno="2" ItemType="File" Filename="MApplication.java" Actualpath="\F:\MApplication.java" Filepath="F:\MApplication.java" Id="2" Filesize="34476" Operation="1" Description="" uplaodparams="" updateparams=""/>
</Items>
</FSRootNode>



Now: I need to Parse and and get all values in my both java bean class. for Item and ItemUpload.

Please help me How o parse this type of XML ??



Regards,
Amardeep
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Searching in Google will yield thousands of examples for parsing a XML file.
Please first research and if you face any problems, do let us know.


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
Amarhzb Salk
Greenhorn

Joined: Feb 28, 2012
Posts: 9
Hi Sumit,

I searched a lot for xml like
<File Filename="M3.TIF" path="F:\FolderTest\M3.TIF" Actualpath="\FolderTest" Id="1" Filesize="674748" Operation="1" Description="" uplaodparams="" updateparams=""/>
I got some Example like below code::



Still my requirement not full fill I also want POJO Values to be updated while parsing. but in this respect I'm not able to do so. Please provide me some hint or any better links, if possible.

Thanks
Amardeep
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You got to read more about DOM and the necessary methods to handle the document.

Lets say the below is a simple representation of your xml.

My understanding is that you want to fill your Item POJO with attributes in the Item node and ItemUpload with attributes in the File node.

Is that correct?
Amarhzb Salk
Greenhorn

Joined: Feb 28, 2012
Posts: 9
Hi John,

yes you are correct, But that xml that you have created is not like that.

My XML should be like this::
<FSRootNode Sessionid="" Id="" Numfiles="2">
<Items>
<Item Itemno="1" ItemType="Folder" name="FolderTest" Actualpath="" totalfilecount="4" ParentFolderID="0"/>
<File Filename="M2.TIF" path="F:\FolderTest\M2.TIF" Actualpath="\FolderTest" Id="1" Filesize="592758" Operation="1" Description="" uplaodparams="" updateparams=""/>
<File Filename="M3.TIF" path="F:\FolderTest\M3.TIF" Actualpath="\FolderTest" Id="1" Filesize="674748" Operation="1" Description="" uplaodparams="" updateparams=""/>
<SubFolder Itemno="1" ItemType="Folder" name="New folder" Actualpath="\FolderTest">
<File Filename="MApplet.java" path="F:\FolderTest\New folder\MApplet.java" Actualpath="\FolderTest\New folder" Id="1" Filesize="41703" Operation="1" Description="" uplaodparams="" updateparams=""/>
<File Filename="MApplication.java" path="F:\FolderTest\New folder\MApplication.java" Actualpath="\FolderTest\New folder" Id="1" Filesize="34476" Operation="1" Description="" uplaodparams="" updateparams=""/></SubFolder>
<Item Itemno="2" ItemType="File" Filename="MApplication.java" Actualpath="\F:\MApplication.java" Filepath="F:\MApplication.java" Id="2" Filesize="34476" Operation="1" Description="" uplaodparams="" updateparams=""/>
</Items>
</FSRootNode>

Because I'm getting xml from third party.

Any Idea! either DOM Parser or JAXB.


Regards,
Amardeep.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
That's fine. This is how I would proceed.
1. Make a DOM document for the XML.
2. Navigate to the necessary node (Items or File).
3. Parse the node's attributes for required information.

I have shortened your code to do #1 and #2. Follow the steps and read about the Node's methods which should help you further - Node API



There are other handful of methods in Node and Element which will help you further.
Amarhzb Salk
Greenhorn

Joined: Feb 28, 2012
Posts: 9
Thanks John, It Works. Thanks a lot dear...
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Sure and next time use code tags for XML too. Select XML in the drop box to format code tags for XML.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to parse XML using Dom parser