Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading an XML file using Java

807599Feb 11 2007 — edited Mar 28 2007
Dear all,

Currently, I'm planning to write a java program that will read from an xml file. This is my XML file:

<?xml version="1.0"?>
<HUMIDITYMEASURE>
<HUMIDITY>32.0</HUMIDITY>
</HUMIDITYMEASURE>

Now, here is my Java code:

import java.io.*;

class ReadAFile{
public static void main(String[]args){

try{
File myFile=new File("XMLFile.xml");
FileReader fileReader= new FileReader(myFile);

BufferedReader reader = new BufferedReader(fileReader);

String line = "null";

while ((line = reader.readLine()) !=null){


System.out.println(line);
}


reader.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}

So far the program displays the content of the xml file (with the tags). However, my aim is to make the program display the values, not the tags. For example, in this case, i'd like my program to display the value '32.0' rather than:
<?xml version="1.0"?>
<HUMIDITYMEASURE>
<HUMIDITY>32.0</HUMIDITY>
</HUMIDITYMEASURE>

Can anyone provide me with any advice as to how i can do this please? I tried using the tokenizer and split, but to be honest, they dont really work. If anybody has any suggestions, please reply if possible.

Regards,
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 25 2007
Added on Feb 11 2007
6 comments
254 views