• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to parse XML using Dom parser

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Amarhzb Salk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John, It Works. Thanks a lot dear...
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure and next time use code tags for XML too. Select XML in the drop box to format code tags for XML.
 
reply
    Bookmark Topic Watch Topic
  • New Topic