rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 learn more   get in touch 

Logo - I Build Search
Mar 29
2009

XML DOM in Java digg

Of late, I have been working with Java. And one of the issues that I faced was XML parsing. With so many libraries available, I decided to stick to jaxp. What follows is sample code to Tree walk over the nodes:

TreeWalk.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.File;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
 
public class Tester {
    public static void main(String args[]) 
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(false);
 
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            org.w3c.dom.Document doc = builder.parse(new File(args[0]));
            NodeList nodes1 = doc.getChildNodes();
            for(int i=0; i<nodes1.getLength(); i++) {
                TreeWalk(nodes1.item(i), 0);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
 
    private static void TreeWalk(Node n, int level) 
    {
        if(n.getNodeType() != Node.TEXT_NODE) {
            for(int i=0; i<level; i++)
                System.out.print("  ");
            System.out.print(n.getNodeName() + ":");
        }
        else {
            System.out.println(n.getNodeValue().trim());
        }
        NodeList list = n.getChildNodes();
        for(int i=0; i<list.getLength(); i++) {
            TreeWalk(list.item(i), level+1);
        }
    }
}

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Latest Articles

Apr
07

Palindromic sub-sequences in python

This bit of python code returns all palindromic subsequences in the input string.

[Read More]
Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it!

[Read More]

Featured Projects

Yahoo Messenger Client for *nix

Yahoo Messenger Client for *nix

Yux is an alternative Yahoo Messenger client for *nix systems that attempts to match the look and feel of the original Windows client.

[Read More]

Indic to English Transliterator

Indic to English Transliterator

Transliteration is the process of converting a word from one language to another while retaining its phonetic characteristics. This application lets you convert a word from any major Indian language (currently supports Hindi, Marathi, Sanskrit and Bengali) to English.

[Read More]

This page and its contents are copyright © 2010, Pravin Paratey.