DOM for Web Services, Part 1
by Faheem Khan
|
Pages: 1, 2
MSXML
MSXML, by Microsoft, implements DOM Level 2 interfaces. In addition MSXML also adds some extended features not included in DOM Level 2.
Some of the MSXML's DOM level 2 interfaces include:
-
IXMLDOMNode interface is the generic DOM node from which all the different types of nodes extend. You will never use this interface directly. Rather you will use the different types of nodes (e.g. element node, attribute node etc.) that extend from the IXMLDOMNode interface.
-
IXMLDOMDocument interface exposes the functionality of a document node, which holds the entire DOM document as its direct or indirect children. You will normally start DOM authoring and processing with this interface.
-
IXMLDOMElement interface defines the functionality of a DOM element node. For example, it has methods to get the list of attributes associated with an element or fetch a particular attribute value, etc.
-
IXMLDOMNodeList interface helps in manipulating a list of DOM nodes (such as all child element nodes of an XML element). It contains methods to iterate through the child element list and find a particular node in the list.
-
IXMLDOMText interface exposes methods to manipulate the textual content of an XML element.
-
IXMLDOMComment interface is used to play with comments in an XML file.
-
IXMLDOMAttribute interface represents the functionality of an attribute node (for example to edit an attribute value).
In addition, MSXML also implements the HTML-specific DOM interfaces for
HTML authoring and processing. The HTML-specific interfaces provide simple
set and get methods to populate and HTML body with <a>,
<p>, <table>,
<form>, <input> and other HTML
elements.
You can use MSXML on both client and server sides. On the client side, you will use MSXML inside Microsoft Internet Explorer. For example, if you want to generate the HTML file of Listing 2 from the WSDL file of Listing 1, you can generate JavaScript from the server side that will use MSXML to produce HTML on the client side.
Or otherwise you can directly generate HTML on the client side using the same DOM features of MSXML.
Have a look at Listing 4,
which is meant to introduce XML processing with MSXML. Listing 4 is actually an HTML file
with a simple JavaScript method named
FindServiceDocumentation, which reads the contents of
the documentation element within the
service element of Listing 1 and prompts the comments to
the user as a browser alert.
Following is a brief explanation of how the
FindServiceDocumentation method in Listing 4 works using MSXML:
-
The first line in the
FindServiceDocumentationmethod instantiates an MSXML object. -
The second, third, and fourth lines of code method are variable declarations, which we will use to hold different DOM element nodes.
-
The fifth line (
xmlDoc.load) loads a WSDL file into DOM. -
The next line is an
ifstatement, which checks whether there were any errors while loading the XML document. The most probable error is perhaps that the XML that you want to load into DOM is not well formed. -
If there were no problems while loading XML into DOM, we will read the root element and check if it is
definitions. If it is not definitions, we are not interested in further processing. -
If the root element is definitions, we will try to find its
serviceelement. Note that the lineserviceElement = definitionsElement.getElementsByTagName("service").item(0)actually works in two steps. First thegetElementsByTagNamereturns a list of allserviceelements and then the methoditem(0)returns the first element from the list. -
Similarly, we read the
documentationelement in theserviceelement. -
Next look at the
alertline, which containsdocumentationElement.firstChild.nodeValuemethod call. This method works in two steps. ThedocumentationElement.firstChildmethod call fetches the first child node of thedocumentationelement. The first child of thedocumentationelement is the required text node that we were looking for. ThenodeValueproperty returns the value of the text node as a textual string. The alert statement displays the textual string as an alert.
We have kept Listing 4 and its explanation very simple and brief, as this is only an introduction. Real world XML authoring and processing is never so simple. We will take this example further in the second article of this series and demonstrate the use of MSXML for both XML and HTML processing as well as authoring in detail.
Xerces
Xerces is a Java-based DOM Level 2 implementation by Apache. In addition Xerces partially supports DOM Level 3. You can create Java-based SOAP client side applications using Xerces, however it is mostly used on the server side.
Xerces interfaces are similar to the interfaces that you have seen while discussing MSXML, which is natural to expect, as both are DOM implementations.
The third article of this series will demonstrate the use of Xerces in detail. For the moment, just notice the following points Xerces interfaces:
-
Almost all interfaces extend from the Node interface.
-
The Document interface allows you to manipulate the complete XML document. Normally you will use the
Documentinterface to get the root element of the XML document. You can also use this interface to import a node from some other DOM document into this document. -
The Element interface allows you to process or author elements (e.g. adding attributes to or removing attributes from an element).
-
The
NodeListinterface is used to manipulate a list of nodes.
We have provided a brief introduction of MSXML and Xerces. The second article will demonstrate the use of MSXML, while the third article will demonstrate the use of Xerces. Both the second and third articles will use the web services usage models and XML authoring and processing requirements as an example application scenario to demonstrate the use of DOM features.
Resources:
|