Document Object Model DOM is a subject to change and query documents by programming. Since the W3C organization provides a unified DOM interface, this makes various language cross-platform applications possible. If an application is based on DOM, we can do not have to care about what language implemented, DOM for various languages A unified object, attribute, method, and event are displayed. The DOM is a tree structure in the XML document in memory. When an XML file is loaded into the XML parser, a corresponding tree is established in memory. MSXML4.0 fully supports DOM, and provides an easy-to-use object model interacts with XMLDOM trees in memory so that we can easily manipulate the XML document through applications or scripts.
Three document models: Linear model: Good at handling static documents, like books, but if this book wants to change the format, or add content, change the number of pages, this model will be powerless. Tree model. Object Model: The XML object model is a collection of objects. With it we can operate data in the XML document, you can also see the XML object model as a tree, there is a corresponding node, each node contains the corresponding document data. However, it adds an object-specific attribute method and event equivalent to the tree model.
The following examples are expanded around this XML XML Version = "1.0"?>
Persons>
Persons>
Persons>
Persons>
People> XML document node ixmldomnode domnode is the basic object in the DOM. Elements, attributes, comments, process instructions, and other document components can be considered ixmldomnode. In fact, the DomDocument object itself is also an ixmldomnode object. The interfaces listed below have inherited his methods and properties. Domdocument ixmldomelement ixmldomattribute ixmldomdomtomtystepe ixmldomentity ixmldomtext document node properties: LastChild; NextSibling: Points to the next brothers node of the same parent node, read-only properties. XML: Format this node and all child nodes in XML format, read-only properties.
VAR XMLDoc = New ActiveXObject ("msxml2.domdocument.4.0");
Xmldoc.async = false;
XMLDoc.Load ("*. xml");
VAR nNode = Xmldoc.creatNode (1, "new adding element node", "");
VAR nNodeText = Xmldoc.creatNode (3, "Text Test", ""
XmlDoc.documentelement.Appendchild (nNode);
XmlDoc.documentelement.lastchild.Appendchild (nnottext);
WINDOW.Alert (xmldoc.documentelement.lastchild.xml ());
script>
Document Node Method: SelectNode: Returns the set of nodes that meet the requirements based on the match mode operation. SelectSinglenode: Returns the first node that meets the requirements based on the match mode operation. Var root = xmldoc.documentelement;
Var Childs = root.selectnodes ("persons / name");
For (var i = 0; i { Document.writeln (Childs [i] .text); } XML Document Object Domdocument object DomDocument object is the foundation of XMLDOM, we can use the properties and methods it exposed to browse, query, and modify the contents and structures of the XML document. DomDocument represents the top-level node of the tree, which implements all basic methods for the DOM document, and provides additional member functions to support XSLT. It creates a document object, all other objects can be created based on this document object. The DomDocument object implements the IXmldomDocument interface. The properties of the document object: async: Indicates if the XML document can be downloaded asynchronously. Attributes: Contains a list of properties, read-only properties. BaseName: Returns the tag name, read-only properties that contain nodes that contain namespaces. ChildNodes: Returns a list of subtitches, read-only properties. DocumentElement: Return to the root element, readable and write properties. Str = xmldoc.documentelement.childnodes [0] .basename; Result: "Person" Var childs = xmldoc.documentelement.childnodes; For (var i = 0; i { Document.writeln (Childs [i] .text); } Method of document objects: appendchild; creatnode: var mydomNode = xmldoc.documentelement.creatNode (type, name, namespaceuri) TYPE: Node type 1 element node; 2 attribute node; 3 text node. Name: Name Name. Namespaceuri: Nodespace belongs. GetElementBytagname (); select the element according to the tabette.