Solved

Java Parsing XML

Posted on 2004-09-02
7
207 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now