471,107 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,107 software developers and data experts.

Parsing and Displaying XML Document

Hi everyone,
This is a brief description that I have provided for parsing & displaying an XML document using DOM API. Please feel free to post your own comments & views regarding this discussion. Thank you.

The first step of parsing an XML document is to import the DOM API related classes such as :-
java.io.* which contains all the interfaces to perform an I/O operation.
org.xml.sax.* which contains all the interfaces for the SAX parser.
org.xml.sax.helpers.* which contains the class that handles the SAX events.
org.w3c.dom.* which contains the Document class & various classes for DOM components.
Are there any more classes that are required that need to be imported before parsing the Document ?

The next step I think is the actual parsing of the XML document. We need to first create an object of the DocumentBuilderFactory class & then create an object of the DocumentBuilder class & then finally call the parse() method of the DocumentBuilder class to parse the XML document.

The final step is to identify the various types of DOM nodes. The various types of nodes of the DOM tree that correspond to the various components of the XML documents can be identified. Node interface represent the nodes of a DOM tree. The ELEMENT_NODE field of the node interface represents an element node of the DOM tree. u can use the foll code to identify the different types of nodes of the DOM tree :-
Expand|Select|Wrap|Line Numbers
  1. private void printNode(Node nd)
  2. {
  3.          int type = nd.getNodeType();
  4.          switch (type)
  5.          {
  6.            case Node.ELEMENT_NODE:
  7.                         out.print("Element Node");
  8.                         println_Com(nd);
  9.                         break;
  10.            case Node.TEXT_NODE:
  11.                         out.print("Text Node");
  12.                         println_Com(nd);
  13.                         break;
  14.                -------------------
  15.  
In the preceding code snippet, the println_Com() method is called to print the node name & the node value. The println_Com method calls the getNodeName() & getNodeValue methods of the Node interface to obtain the name & value of a node.
Expand|Select|Wrap|Line Numbers
  1. private void println_Com(Node nd)
  2. {
  3.   String nodeName = nd.getNodeName();
  4.   if(nodeName != " ")
  5.           out.print(" Node Name = \"" + nodeName + "\"");
  6.   String nameSpace = nd.getNodeValue();
  7.   if(nameSpace != null)
  8.   {
  9.     out.print(" nodeValue =");
  10.     out.print("\"" + nameSpace + "\"");
  11.   }
  12.   ------------------
  13.   ------------------
  14.  
Jan 22 '08 #1
2 2381
jkmyoung
2,057 Expert 2GB
Does this example only cover element and text nodes, or is there more code to cover attributes, comments, namespaces, etc?

Wouldn't you get duplicated output by using the getNodeValue() function? eg once for element, and 2nd time for text?

I would not use nameSpace as the name of the value, as this will cause confusion with namespaces. nodeVal perhaps?

Is this article supposed to be parsing XML in a forward only, event-driven mode (eg SAX), or based on building a tree structure with DOM?
Jan 22 '08 #2
Does this example only cover element and text nodes, or is there more code to cover attributes, comments, namespaces, etc?

Wouldn't you get duplicated output by using the getNodeValue() function? eg once for element, and 2nd time for text?

I would not use nameSpace as the name of the value, as this will cause confusion with namespaces. nodeVal perhaps?

Is this article supposed to be parsing XML in a forward only, event-driven mode (eg SAX), or based on building a tree structure with DOM?
Hi jkmyoung,
Actually the code snippets I have provided are a part of an XML document parsing technique. I am parsing my XML document using SAX API & DOM API. The examples that I have provided are of DOM API. The example covers text nodes, element nodes, atrribute nodes, comments & namespaces as well. I would not get a duplicated output by using the getNodeValue() function as each time a different element & text would be displayed in the DOM tree. Thank You.
Jan 23 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by Terry | last post: by
4 posts views Thread by Erik Moore | last post: by
3 posts views Thread by David Svoboda | last post: by
5 posts views Thread by randy | last post: by
1 post views Thread by avpkills2002 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.