• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading a XML File using Java

 
Venish Joe
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a XML file, a sample is given below. I need to read this XML file using java and get all the values. Can you tell me how I can do this ? Some sample code will be great.

XML
--------
<?xml version="1.0" encoding="UTF-8"?>
<config Name="appname" Version="0.1" >
<child1 Name="Child1">
<child-title Name="This is an XML Child" />
<child-version>1.0</child-version>
</child1>
</config>

Thanks
Venish Joe
 
aslam parveez
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls be a little more clear in what exactly you want to do ?

You have said you want to read the XML file and get all the values (what values you want to get ?).

An XML file can be parsed in Java using SAX parser and DOM Parser , but what exactly you want to parse in the given XML file ?
 
Venish Joe
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry for being unclear. I need the values from all the tags. That is for the line <config Name="appname" Version="0.1" >, I want to get the value of the attributes Name and Version. This applies to all the tags.

Thanks
Venish Joe
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ Removed my original post to reflect the fact that the screen name is OK. I didn't know that "Joe" is a proper last name - now I do. Sorry about that. ]
[ June 21, 2006: Message edited by: Ulf Dittmer ]
 
Venish Joe
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer,

If Iam correct, the display naming policy states that the display name should be First Name Space Last Name. My First Name is Venish and Last Name is Joe. I don't know if I missed out something. Please advice if Iam missing some points.

Thanks
Venish Joe
 
aslam parveez
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the below link for a sample program to what evere you want to do.

http://developers.sun.com/sw/building/codesamples/sax/index.html
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
 
Jay Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try XMLBeans . Given a schema and the xml, getting the values is pretty straight forward. Download XMLBeans from Apache. This converts the xml nodes to objects. Now, you can manipulate the objects the get the required data.

Regards,
Jay.
 
anilkumar choudary
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iam sorry tat u didnt mention clear info about wat u want to read u can parse an xml document by using DOM and sax parser if u wnt for read only purpose go sax parser and if u want to modify the data in an xml document go for DOM parser ok
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic