Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Parsing XML

Posted on 2004-09-02
7
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

721 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