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
Solved

Using an XML document

Posted on 2007-11-28
10
220 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 63
Spring Framework HTTPSession management 1 45
object oriented programming comparison 5 77
ejb entity bean example 2 11
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

856 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