BASIC OF DOM
DOM is the Document Object Model Document Object Model acronym. According to W3C DOM specification (http://www.w3.org/DOM/), and DOM is a browser, platform, language-dependent interface, allows you to visit other pages of standard components. Simple to understand, DOM Javascript resolved Netscape's and Microsoft's Jscript the conflict between the granting web designers and developers a standard method to enable them to visit their site data, and performance of the scripts like.
DOM is organized hierarchical structure of the node or collection of information clips. This hierarchical structure allows developers in the tree navigation for specific information. Analysis of the structure is usually the entire document to load and structural hierarchy, and then to do any work. Because it is based on the information level, and thus is considered DOM-based or object-based tree.
For particularly large files, parsing and loading the entire document may be very slow and very consumption of resources, the use of other means to handle such data will be even better. These event-based model, such as the Simple API for XML (SAX), applies to the handling of data flow, that is, with the flow of data and order data processing. Event-based API eliminates the tree structure in memory needs, but does not allow developers to modify the original documents in the actual data.
SAX model, and so will be the order from beginning to end, the entire XML document analysis, there are a node or the beginning or the end, we will have a time, programmers can register to the event handler of all nodes for processing.
On the other hand, DOM also provides an API, allowing developers to add, edit, move or delete arbitrary location of the tree nodes, thereby creating a invoked procedures. The model is based on the tree data structure, and he must be in use before loading the entire document or at least well-structured document fragment
Parser is a software application designed for the analysis of documents (XML document refers to here), as well as some specific information on the matter. SAX such as this event-based API, the parser will be a listener sent incident. Based on this, such as DOM tree API, the parser will be constructed in memory of a data tree.
As the DOM API
DOM Level 1 from the start, DOM API contains a number of interfaces for XML documents that can be found in all different types of information. It also includes the use of these objects by the necessary methods and properties.
Level 1 includes HTML and XML 1.0 support, each HTML element is expressed as an interface. It includes for adding, editing, mobile nodes and read the information contained in the method, and so on. However, it did not include the name of the XML space (XML Namespace) support, XML name space to provide the information in the document segmentation capability.
DOM Level 2 adds name space support. Level 1 Level 2 of the expansion, allowing developers to detect and may be applicable to the use of the name of a node spatial information. Level 2 also added several new modules to support cascading style sheets, events and enhance the operation of the trees.
Is currently in final stages of DOM Level 3, including the creation of Document object (the previous version will be left to the realization of this task, creating common application makes very difficult) to better support, and the name space support, as well as to deal with documents Loading and preservation, as well as validation of the new XPath module; XPath in the XSL transformation (XSL Transformation), and other XML technology used to select a node means.
DOM as a means of the development of modular, you must know that they want to use it to the characteristics of being used to achieve the support of the DOM.
Determining availability of
DOM recommended standard modules allow nature to be realized, including the selection of some of the products, thus the use of a particular characteristic, first determine whether the features available may be necessary. The Guide uses only DOM Level 2 Core API, but start your own projects, learn how to detect features some help.
DOM defined in the interface is one of DOMImplementation. By using hasFeature () method, you determine whether or not a particular characteristic supported. DOM Level 2 does not exist to create DOMImplementation the standard method, but the following code will demonstrate how to use hasFeature () to determine the DOM Level 2 in a style sheet module in the Java application supported.
Import javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.DocumentBuilder;
Import org.w3c.dom.DOMImplementation;
(Public class ShowDomImpl
Public static void main (String args []) (
Try (
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
DocumentBuilder docb = dbf.newDocumentBuilder ();
DOMImplementation domImpl = docb.getDOMImplementation ();
If (domImpl.hasFeature ( "StyleSheets", "2.0")) (
System.out.println ( "Style Sheets are supported.");
Else ()
System.out.println ( "Style Sheets are not supported.");
)
) Catch (Exception e) ()
)
)
(DOM Level 3 will be used to create DOMImplementation including the standard method.)
This tutorial will use a single document to show DOM Level 2 Core API targets and methods.
The basic XML document
The Directory of all the examples are as follows with a node contains the XML document, which said that the importation of a commercial system orders. Recall that the XML document elements include:
XML statement: Statement <? Xml version = "1.0" encoding = "UTF-8"?> This document will be defined as XML documents. Specified in the statement of a character encoding is not uncommon, as shown below. In this way, no matter the language used in XML documents or what character encoding, as long as the parser to understand the specific coding, it can be correctly read the XML file.
DOCTYPE statement: XML is human-machine to facilitate the exchange of information between the means, but to enable it to work smoothly, there must be a common glossary. Optional DOCTYPE statement can be used to designate a document should be used to compare with this document (in this case, orders.dtd) to ensure that there would not be any confusion or loss of information (for example, lost a userid or misspellings a certain element name). In this way, the document dealt with as effectively documents. Check the validity of success is not necessary for XML, as the examples in fact has been omitted from the document DOCTYPE statement.
Data itself: the data in the XML document must be included in a single root element, elements such as the following orders. For XML documents to be addressed, it must be a good format (well-formed).
<? Xml version = "1.0" encoding = "UTF-8" />
<! DOCTYPE ORDERS SYSTEM "orders.dtd" />
<orders>
<order>
<customerid Limit="1000" /> 12341 </ customerid>
<status> Pending </ status>
<item Instock="Y" itemid="SA15" />
<name> Silver Show Saddle, 16 inch </ name>
<price> 825.00 </ price>
<qty> 1 </ qty>
</ Item>
<item Instock="N" itemid="C49" />
<name> Premium Cinch </ name>
<price> 49.00 </ price>
<qty> 1 </ qty>
</ Item>
</ Order>
<order>
<customerid Limit="150" /> 251222 </ customerid>
<status> Pending </ status>
<item Instock="Y" itemid="WB78" />
<name> Winter Blanket (78 inch) </ name>
<price> 20 </ price>
<qty> 10 </ qty>
</ Item>
</ Order>
</ Orders>
In the DOM, the use of XML message means that it will be divided into the first node.
