C # discovery journey Article 10 Object Model
Yuan Yongfu
Course Series
To help you better understand and use C #, we will begin this series of technical lectures on the "C # discovery Journey. Considering that most of you are engaged in WEB database development, the so-called discovery is to discover areas we are not familiar with, so the content of this series of lectures will be C # applications other than WEB database development. Currently, the main content of the plan is graphic development and XML development, and multiple courses are planned and arranged. In the future C # discovery journey, we will explore and discover other unknown fields of C # in a step-by-step manner, more in-depth understanding and understanding of using C # for software development, broaden our horizons, and enhance our comprehensive software development capabilities.
Course description
This course focuses on the object model concept of the document. It is theoretically strong and requires careful consideration. There is no demonstration program. This course is the final part of the C # discovery travel series course, however, it does not rule out the possibility that I will launch a sequent C # discovery tour in the future.
Articles published in this series of courses include:
C # discovery tour lecture C #-XML Development
C # Journey to Discovery C #-XSLT Development
C # Journey to Discovery Lecture 3 use C # To develop XSLT-based code generators
C # getting started with Windows Graphics Development
C # journey of discovery Part 5 basics of graph Development
C # discovery tour lecture 6 C # intermediate level of graph Development
C # discovery journey Lecture 7 C # advanced graphic development
C # Journey to Discovery lecture 8 ASP. NET graphic development with hyperlink pie chart
C # Learning ASP. NET Verification Code Technology
C # discovery journey Article 10 Object Model
Document Object Model Definition
The Document Object Model (DOM) is an important software design Model and is not a programming technique. In this case, if you do not recognize DOM in your life, you will be called a hero. You should know more about its capabilities.
The Document Object Model is a concentrated embodiment of object-oriented programming technology. Without a complete object-oriented programming idea, it is impossible for developers to understand and develop document object models. If developers can develop document object models, it means that he/she fully understands and master the object-oriented programming ideas.
W3C International defines the Document Object Model as follows (from http://www.w3.org/DOM)
The Document Object Model is a platform-and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. the document can be further processed and the results of that processing can be ininitialized back into the presented page. this is an overview of DOM-related materials here at W3C and around the web.
My personal English translation is as follows:
The Document Object Model is a platform or language-neutral interface that can be used by programs or scripts to access and update structured documents. These documents can be further processed, and the processing results can form a valid page. This is W3C's general view of the principles of the Document Object Model on the web.
Document
Let's gradually understand this definition. First, what is a document. A document is a data packet that is organized together in a structure. For example, a MS Word document is a document that contains many characters, images, and paragraph data and is saved in a binary file using a specific organizational structure. For example, an HTML document also contains text, images, links, and form data, and can form a hierarchical structure stored in a string or text file according to the Public HTML syntax.
Structured document
Second, what is a structured document, many documents have a hierarchical structure of content organization. That is, the content abstraction can form a tree structure, such as HTML documents and XML documents.
For a common text document, it is a string from the developer's perspective, and there is no mutual relationship between each character or substring. In this case, a common text document is not a structured document. If you add a syntax analysis that supports a hierarchical structure to this text document, the text document becomes a hierarchical document.
If the HTML document does not have an HTML syntax explanation, it is a plain text document. It is a common string. if you add the HTML syntax to this string, this plain text document immediately becomes a structured document with considerable complexity. In the same way, plain text documents such as XML files and SQL statements are a common string before they are interpreted. After being interpreted, they become a useful XMLDOM and SQLDOM.
Programming Interface
DOM is an interface or platform. For software developers, this so-called interface or platform is a programming interface (API). There are many APIs that can directly call functions, for example, the traditional Win32API function exposes a programmable object, and the programming object has a public property method or event, such as a COM interface or.. NET class library interface. There are also WEB-based WebService programming interfaces. In general, for DOM, the programming interface is represented by the programming interface of the programmable object, that is, several programmable objects are released to the DOM, other applications or scripting languages can call public members of these programmable objects.
An important feature of DOM is that an application or script can use it to access and update structured documents. This means that DOM provides a number of programmable objects externally. These objects use various internal means to ensure that they correspond to a part of the structured document, in particular, the attributes of the object and the attributes of the document fragment maintain a ing relationship. The external program obtains the properties of a programmable object. After the DOM internal ing relationship, it is equivalent to obtaining an attribute of a document segment. The external program modifies the properties of a programmable object, after the internal ing, the attributes of a document segment are modified. Therefore, DOM is a proxy between application software and structured documents. The application software uses DOM to view the content of documents and modify the content of documents by DOM "Hitting the barriers.
Valid page
After structured documents are processed, a valid page can be formed, that is, structured documents can be displayed on the user interface. With the help of DOM, general applications can draw contents of structured documents on the user interface. For example, a WEB browser uses html dom to draw HTML document styles on the user interface. Some structured documents do not have user interfaces, such as XML documents, but they have programmable user interfaces. Other application systems can use this programmable user interface to find out what content is in XML documents.
Document Object models can be language-neutral, that is, cross-language cross-platform, such as HTML and XML document object models. With the efforts of W3C international standards organizations, it is already the most typical cross-language cross-platform Document Object Model. We can use any platform or language to access XML and HTML using the same method and interface, for example, JAVA in Linux and Windows. NET, VB, JavaScript in various browsers, or even VBA in MS Office, the process of accessing the xml dom must be similar, it is easy to use the same processing flow to implement the same function. The advantage of doing so is to facilitate program porting and data exchange between various systems.
We can think that if a document has a life, it will be almost dead when it is stored in a file, but it will be awakened once it is attached to the DOM, it is a live task, which allows application software to dispatch and exert any function.
The above is my personal theoretical understanding of the Document Object Model. Next I will talk about how to understand the Document Object Model in software development.
Understand the Document Object Model
The Document Object Model is a specification that implements two types of DOM, CodeDom and XMLDom in the Microsoft. NET Framework class library. The namespace System. CodeDom defines the CodeDom, And the namespace System. Xml defines the XML Dom. Here we will explain it using the familiar XML Dom.
XmlDom Structure Research
This figure shows the ing between an XML document and an XML object.
After studying the previous courses, you have mastered the basic XML syntax. Therefore, we will not detail the syntax structure of this XML document here. In this ing graph, we use the XmlDocument object to film the entire XML document; Use XmlDeclaration to film "<? ?>" The XML declaration defined. Similarly, we use the XmlElement object to film an XML element; Use XmlText to film plain text data in the XML document; and use XmlAttribute to film XML Attribute fragments; use XmlComment to film XML comments.
After the above picture, developers can call each XML object by programming to modify the corresponding part of the XML document. For example, by calling the Value Attribute of the XmlText type, you can easily obtain and modify plain text data fragments in the XML document.
The XmlDocument type defines many functions starting with "Create" to Create a new Xml object. For example, you can use the CreateElement function to Create an XmlElement object, add it to the ChildNodes list of an existing XmlElement. This operation is equivalent to adding elements to the XML document. You can also delete objects in ChildNodes of an existing XmlElement, this is equivalent to deleting a piece from the XML document.
Through the ing between the programming object and the XML document, we will convert the operations on the XML document into operations on one programming object. We can easily use programming to process XML documents with complex syntax structures.
Evaluate the knowledge of XML documents, such as XmlDocuement, XmlElement, and XmlText. We can find that they are all derived from the XmlNode type. XmlNode defines common programming interfaces for Xml objects, other Xml types are derived from XmlNode and extended according to their corresponding XML fragment types. In this way, we can easily use XmlNode as a strong type to traverse XML documents.
In fact, there are complex object inheritance relationships under the namespace System. Xml. For example, there is such a derived link linked list XmlNode-> xmlcharacter node-> XmlCharacterData-> XmlText. Why should we design such a long derivative linked list, what functions should each layer implement, what interfaces should be reserved, and how should each layer expand members of the upper layer. All these are the basic skills and software design ideas that test the developer's object-oriented programming technology. Therefore, if a developer can understand and design a complex and reasonable DOM, then this person must be a master of software design.
In general, we program the objects stored in the same list. They have the same type, so we can safely use forced type conversion to obtain any element in the list. However, in XML document object types, the object types in the same list may be inconsistent. Unconditional forced type conversion is unreliable and error-prone. Therefore, type determination is required before forced conversion. In addition, this phenomenon is common in other document object models. For example, in HMTLDOM, a subnode of an HTML Node also has a similar situation. Therefore, pay attention to this when programming document object models. However, because all Xml object types are derived from XmlNode, we can use the XmlNode type reliably to traverse the XML document structure.
XmlDom contains an XML document parser Based on XmlReader. by calling the Load method of XmlDocument, the system can Load and parse text data from a file, automatically constructs an Xml Object Tree Structure with XmlDocument as the root node. XmlDom also contains an XML document writer based on XmlWriter. After we modify the tree structure of the XML object, you can call the Save method of XmlDocument to Save the modified Xml document to a file. This completes the modification to the Xml document.
If we don't need XMLDOM, how can we program to access XML documents? Obviously, the XML document is a string. We need to perform complex string operations on the string containing the XML content. The software development process is complex and inefficient, XML technology cannot be popularized because it is difficult to develop and use.
DOM Concept
After studying XMLDOM, We can summarize some basic programming features of DOM:
- All document object types are derived from a basic type, and document object types implement various features on the basic type.
- Each document object maps to a part of the document. modifying a Document Object is equivalent to modifying the document part itself.
- The document object can have subnodes, thus forming a hierarchical tree structure of objects.
- There is a top-level object used to represent the entire document. You can control the entire document at a macro level, which is also the only entry for applications to access the tree structure of document objects.
- The Document Object Model is programmable, and applications access structured documents by accessing the Document Object Model.
- Most types of DOM can save the entire document structure to a file, or reproduce the entire document structure from a file. This counts as object serialization. However, there may be exceptions.
We can find that other DOM basically implement the above programming features, such as the CodeDom and html dom supported by the namespace System. CodeDom.
DOM concept Extension
XMLDOM is a rigorous DOM programming feature. However, in actual development and application, we can simplify it according to various requirements and develop a variety of less rigorous DOM. For example, in the Microsoft. NET standard library, there are many less rigorous DOM structures. For example, the namespace System. fileSystemInfo, FileInfo, and DirectoryInfo under IO constitute the file System DOM; namespace System. the types in Reflection constitute the Assembly DOM with Assembly as the root node; namespace System. windows. the types in Forms constitute the WinFormDOM with Form as the Document Object and Control as the basic type; namespace System. web. the types in the UI and its sub-namespaces constitute the ASP.. NET page DOM. It can be said that the tree structure of an object can be considered as an unrigorous DOM structure.
After learning XMLDOM, We can imitate the design of their own DOM, in the Open Source website http://sourceforge.net, using the keyword DOM for search, will find a lot of open source projects, most of these open-source projects are related to various DOM types.
Documents are data, and DOM processing documents are actually data processing. Further, we can refer to DOM design ideas in various application systems, such as developing WinForm or ASP. NET, these user interface controls can also be seen as DOM. The electronic medical records Document Object Model can be designed for the electronic disease duration, and the customer Document Object Model can be designed for the customer relationship management system. In a word, we can consider designing a XX Document Object Model when creating a XX system.
Summary
Document Object Model is a complex software design technology. However, it is powerful, well-defined, logically clear, and a good Document Object Model is reasonable, is a high degree of abstraction of business logic data. Everyone should study hard and study more. This is good for improving software design capabilities and setting up their own object-oriented programming ideas.