Solved

Java Parsing XML

Posted on 2004-09-02
7
195 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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