Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using an XML document

Posted on 2007-11-28
10
Medium Priority
?
227 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

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 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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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…
Suggested Courses

782 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