473,387 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2628
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

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

Similar topics

0
by: Prakash | last post by:
Hi all, I am trying a parse a xml document containing japanese text by constructing a DOMBuilder object. The document created after parsing is empty. If the xml document does not contain...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
4
by: Erik Moore | last post by:
I am both producing and parsing an xml document that needs to be validated against a schema. I wanted some consumers of the document to have the option of not performing a validation, so I left the...
3
by: David Svoboda | last post by:
I have a server program that takes commands and acts on them. The server program can also take these commands from an input file or standard input (mainly for testing purposes). As such, I often...
5
by: randy | last post by:
Can some point me to a good example of parsing XML using C# 2.0? Thanks
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
33
by: buss123 | last post by:
Hi all, combo box script code was working in IE perfectly with all modes but OnChange event was not working in FireFox(editable mode, if we select valuese that combo box values r...
1
by: avpkills2002 | last post by:
I seem to be getting this weird problem in Internet explorer. I have written a code for parsing a XML file and displaying the output. The code works perfectly fine with ffx(Firefox).However is not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.