Solved

Using an XML document

Posted on 2007-11-28
10
223 Views
Last Modified: 2010-03-30
Hi,

I have some code that builds an XML document object, looks like:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validating);

    // Create the builder and parse the file
    Document doc = factory.newDocumentBuilder().parse(new File(filename));
    return doc;


It works fine. I made a dummy XML document that looks like:

    <xml>
        <a>a
            <a0>a0</a0>
            <a1>a1</a1>
            <a2>a2</a2>
        </a>
        <b>b
            <b0>b0</b0>
            <b1>b1</b1>
        </b>
    </xml>

Now how do I traverse the nodes of the document using the created 'doc' object? I want to be able to hit all the nodes in the document. Something like:

       System.out.println("Here's a node value: " + doc.xml.a.a1);

Thanks
0
Comment
Question by:DJ_AM_Juicebox
[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
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20368478
0
 
LVL 1

Expert Comment

by:arunyeshi2000
ID: 20368490
You can do something like this..

printNodes(doc.getElementsByTagName("xml"));

//Recursive function
printNodes(NodeList nodelist){
    //Get the root Node
    NodeList nodelist = doc.getElementsByTagName(nodelist.item(0));
 
    //Loop thru and print the node values.
    for(int i=o;i<nodelist.getLength();i++){
    System.out.println(nodelist.item(i));
    printNodes(nodelist)
}

0
 
LVL 9

Expert Comment

by:ysnky
ID: 20368718
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:DJ_AM_Juicebox
ID: 20371108
Hi,

The only thing I don't get is how to get the text value and only the text value of the node. For example I have:

    <weight>50</weight>

and am eventually using the code:

    Node childNode = list.item(i);
    System.out.println(childNode);

how do I extract only the text element of it?

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 20371253
Once you have your text node use:

    Text textNode = (Text) node;
    String text = textNode.getData();
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20371286
ok but if the particular node doesn't have text associated with it, how can I check that (right now I'm getting an exception). I need something like:

if (node.HasText()) {
    // do stuff you listed
}

Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 20371299
if (node instanceof TextNode)
{
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20371324
where is TextNode defined, what do I need to import to use it? Compiler saying it's undefined,

Thanks
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20371353
sorry, typo should have been:

if (node instanceof Text)
{
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20371358
thanks
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

630 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