Solved

Using an XML document

Posted on 2007-11-28
10
215 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maven java path setting 5 50
Understanding websocket example in spring 1 42
groovy example issue 10 70
Problem to start Neon 20 54
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now