Solved

Java Parsing XML

Posted on 2004-09-02
7
217 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 91
micro services vs rest web services 16 105
Java Timer (static) 9 44
Adding multiple JVM environments to RedHat 6 7 21
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

777 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