Solved

Java Parsing XML

Posted on 2004-09-02
7
228 Views
Last Modified: 2011-09-20
Hi,
   Okay, I've tried to do this myself by picking through some tutorials and reading around - no doubt I will go back and re-read them more thoroughly now! However this one thing I don't understand is why this doesn't work :


          NodeList fileName = fileElement.getElementsByTagName("FileName");
          Element fileNameElement = (Element)fileName.item(0);
          System.out.println("File Name : " + ((Node)fileName.item(0)).getNodeValue());
          System.out.println(fileName.item(0));


Line 3  just prints "Null";
Line 4 prints "<FileName>Test</FileName>"

So what I'm wondering is what I've done wrong on Line 3. I was expecting it to print "Test"  but obviously that was very naive of me :)

So, please - put me out of my misery! lol
0
Comment
Question by:petepalmer
  • 4
  • 3
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 11961710
try

fileNameElement.getFirstChild().getNodeValue();
0
 
LVL 92

Expert Comment

by:objects
ID: 11961715
in your case thats:

 System.out.println("File Name : " + ((Node)fileName.item(0)).getFirstChild().getNodeValue());

but as you already have a refence to the element you don't really need to call item() again.

0
 
LVL 1

Author Comment

by:petepalmer
ID: 11961718
That worked fine....

What was wrong with my line?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 11961724
the node value you are after is attached to the first child of the element.
0
 
LVL 1

Author Comment

by:petepalmer
ID: 11961737
My line looked right.... lol

how deceptive! :)
0
 
LVL 1

Author Comment

by:petepalmer
ID: 11961749
Here's an example of the file section :


<File>

<FileName>cs_dust.wad</FileName>
<FileLocation>\</FileLocation>
<MD5Hash>0e2976b3a03627dd2d199575b13a946f</MD5Hash>
<DownloadLocation>www.peteslan.net/cs_dust.wad</DownloadLocation>
<FileSize>8288</FileSize>

</File>

I can get to FileName now....but how do I get to the rest?
0
 
LVL 92

Expert Comment

by:objects
ID: 11961760
you can use getChildNodes() to get the children:

NodeList children = fileElement.getChildNodes();
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question