Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Reading a XML File using Java

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found this by using Google for 5 seconds:

Working with XML: The Java/XML Tutorial
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic