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
- private void printNode(Node nd)
- {
- int type = nd.getNodeType();
- switch (type)
- {
- case Node.ELEMENT_NODE:
- out.print("Element Node");
- println_Com(nd);
- break;
- case Node.TEXT_NODE:
- out.print("Text Node");
- println_Com(nd);
- break;
- -------------------
Expand|Select|Wrap|Line Numbers
- private void println_Com(Node nd)
- {
- String nodeName = nd.getNodeName();
- if(nodeName != " ")
- out.print(" Node Name = \"" + nodeName + "\"");
- String nameSpace = nd.getNodeValue();
- if(nameSpace != null)
- {
- out.print(" nodeValue =");
- out.print("\"" + nameSpace + "\"");
- }
- ------------------
- ------------------