aspose file tools*
The moose likes Java in General and the fly likes Reading a XML File using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading a XML File using Java" Watch "Reading a XML File using Java" New topic
Author

Reading a XML File using Java

Venish Joe
Greenhorn

Joined: Mar 01, 2006
Posts: 14
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

Joined: Apr 25, 2006
Posts: 47
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

Joined: Mar 01, 2006
Posts: 14
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
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
[ 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 ]

Ping & DNS - my free Android networking tools app
Venish Joe
Greenhorn

Joined: Mar 01, 2006
Posts: 14
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

Joined: Apr 25, 2006
Posts: 47
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

Found this by using Google for 5 seconds:

Working with XML: The Java/XML Tutorial


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jay Ram
Ranch Hand

Joined: Dec 13, 2005
Posts: 40
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

Joined: Jun 15, 2006
Posts: 6
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a XML File using Java