473,473 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Parsing and Displaying XML Document

6 New Member
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 2637
jkmyoung
2,057 Recognized Expert Top Contributor
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
nicky123
6 New Member
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.