DOM Node Element Attr Association and difference Summary

Source: Internet
Author: User

Node has several sub-types: Element, Text, Attribute, RootElement, Comment, and Namespace.
Element is a node that can have attributes and subnodes.

---------------------------------------------------------

Node and Element are concepts in two fields.

NODE is relative to the data structure such as TREE. A tree is composed of nodes. For this part, you can refer to the tree chart of discrete mathematics.

ELEMENT is a concept in XML, and <xxx> is an ELEMENT, which is one of the components of XML data.

The concept of ELEMENT and NODE overlaps with each other because XML uses the TREE structure. Therefore, you only need to understand the concept of TREE and their differences.

Finally, I would like to emphasize that they are two concepts in different fields in a strict sense.
 
---------------------------------------------------------
 
DOM (Document Object Model) is a description system for XML data. It uses documents in a tree structure to store XML data. In addition, DOM also includes APIs for parsing and processing XML data.

Before using DOM, you should first understand its structure. The overall structure of DOM is a Composite mode. All XML elements, including documents, elements, attributes, and texts, are a Node in the DOM ). According to the definition of the Composite mode, each Node can accommodate other nodes, so it is easy to form a tree structure.

-------------------------------------------------

Differences between Node and Element in xml:
 
The difference between an element and a node is that an element is defined in a small scope and must be a node containing complete information, such as <div>... </div>. However, a node is not necessarily an element, but an element must be a node.
---------------------------------------------------------
 
The ATTR interface indicates the attributes in the element object. Generally, the value allowed by this attribute is defined in the document-related mode.
 
---------------------------------------------------------
1. What is Dom?
Dom is the abbreviation of Document Object Model. It represents the content of an XML document. It regards XML documents as a series of relationships between nodes and each node as an object, so it is called the Document Object Model.
The DOM specification is defined by W3C and has three levels:
L level 1: defines the vast majority of basic functions
L Level 2: added the namespace definition.
L Level 3: better support for various features
Dom has nothing to do with programming languages, so there are multiple implementations where Java APIs are used (I won't do anything else ), the standard JAXP (Java API for XML parsing) defined by Sun is used, while JAXP is just an interface, which is implemented by calling other specific Resolvers. In addition, JDOM and dom4j are not implemented using JAXP Dom.
Dom regards everything in the document as a node, so it defines a basic interface that is node. Its word interfaces include element, ATTR, text, and so on. It also includes document, that is to say, Dom regards the entire document as a node. Many methods are defined in node, including getfirstchild (), getnextsibling (), getlastnode (), getchildnodes (), getnodename (), getnodetype (), getnodevalue (), getparentnode (), getattributes (), getownerdocuemt (), modify node (insertbefore (), removechild (), appendchild (), replaceChild (), setnodevalue (). These methods are very common, but do not define the method for creating a node. The method for creating a node is defined in its subinterface document (createxxx ()), there are also no methods to get nodes by node name. These methods are defined in element and document (getelementbyid (), Ge Telementbytagname (). Note that the element is obtained directly by name ).
When parsing a document, Dom generates a tree based on the structure of the entire document and stores all the trees in the memory. This not only produces some advantages but also produces some disadvantages. The advantage is that the entire document is always in the memory. We can access any node at any time and we are familiar with tree traversal. The disadvantage is that memory consumption, you must wait until all documents are read into the memory for processing.
Note that the gap between labels in the XML document is also a node (text node) in the tree ).
 
Ii. Dom APIs
1. Create a document
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Documentbuilder builder = factory. newdocumentbuilder ();
Document doc = builder. parse ("abc. xml ");
The process of creating a document is very fixed and takes three steps. Here, DocumentBuilderFactory has some methods to control our parsing, such as whether it must be validated by DTD or Schema, whether to ignore annotations, and whether to ignore namespace:
Factory. setValidating (true );
Factory. setIgnoringComments (true );
Factory. setNamespaceAware (true );
2. Browse the content of XML documents
The first step after obtaining the Document is to obtain the root element, also known as the document element:
Element root = doc. getDocumentElement ();
This is one of the most common methods of Document. However, you can also directly call getFirstChild. After obtaining the root node, you can use various getXXX () methods for browsing. One important problem is that Attr does not belong to any subnode. Therefore, getFirstChild (), getNextSibling (), and getChildNodes () cannot be used to obtain it, you must use a dedicated method getAttributes (), and getAttribute (String) is processed separately.
3. Modify the XML document
Modifications include modifying existing nodes and adding new nodes to the document.
To modify the value of an existing Node, you only need to browse to the target Node and then call Node. setNodeValue () is enough. For Attr, setValue () is called. to modify an existing node, replaceChild () is used. To delete a node, removeChild () is used ().
When creating a new node, as mentioned above, it is created using document. After creation, you can use append () or insertbefore () to insert it to a proper place. In addition, ATTR is always special. To add an attribute to an element, you must use the setattribute () method. to delete an attribute, you must use the removeattribute () method.
4. return values: nodelist and namednodemap
In the get method, some methods return a node list, the most is nodelist, while getattributes () returns namednodemap. Fortunately, the two are completely like a list, all access methods are item (INT index ).
5. Output XML documents
The preceding modified XML document only modifies the tree in the memory and does not affect files on the disk. To truly output data to the disk, it actually has nothing to do with Dom. Instead, it uses filewriter or fileoutputstream to output a document as a file. However, when filewriter. Write () is called, it is not a document object, but a root element object:
Writer. Write (Root );
In addition, you usually need to create a new XML document. Another method of documentbuilder is newdocument (). You can create a new document, and then naturally create a series of nodes and connect them, form a complete document and finally output it.
It is worth noting that we usually need to read a document, browse it, make some modifications, and then write the modifications back to the disk, but there is no way to directly write it back to the original document. It can only output it to a document with the same name, covering the original one.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.