XML programming instance (1)

zhaozj2021-02-08  209

XML programming instance (1)

For some time, XML is used due to the need for work, so some simple research is performed. I would like to write some experience, sharing with you, and I hope that there is a lot of inclusive.

1. What is XML? First, I think you should have a probably knowing what is XML. If you don't have a concept about what is XML, you can look at some related materials, I will not say much.

2. Why do you use XML? In fact, I didn't understand it. Later, I was slowly realized in my work. First, the procedures I have written need to deliver a lot of data structures, such as forms, directory trees, etc. In the past, I think I will define a data structure. This is a matter of quite trouble, and when this structure needs to be updated, when there is a lot of flexibility, it is even more headache, let alone generality and cross-platform. At this time, XML is displayed on the powerful expression of tree structure data. For example, a table data.

11

12

21

22

11 12 21 22

The above description is very simple, and the versatility and cross-platform are very obvious. Of course, there is also the price, that is, the data used to express information is getting bigger. This requires you to trade between performance and scalability.

Again, XML has now been popular, his predecessor (talking is a more suitable) HTML is an evidence, not to say Microsoft's .NET plan is the architecture on the XML.

Finally, because XML is popular, there have been more and more development tools (SDK), such as XML Parser there are many kinds. With these tools, we can develop faster, thereby reducing unnecessary trouble ---, such as the definition of a set of specifications, also develop related operating tools, etc., trouble, and not easy to communicate with the outside world.

3. What development tools are used here, we are facing programmers, and it is prescribed by VC . There are a lot of choices, but the XML Parser SDK choosing Microsoft should be good, at least the document is very detailed (at this point, Microsoft has done a good thing). Of course there are still many other choices, such as Excel on apache.org. For more detailed information, please see www.ibm.com/xml.

basic concepts

In front of XML programming mode, we said that XML's expression ability of the tree is very powerful. We can express an XML document with a tree. For the operation of the document, it is the operation of the tree, this is the DOM (Document Object Model ). However, the DOM has many problems with the processing of the XML document, such as speed slow, so there is another model SAX. Below, we detail the two models respectively.

The DOM model DOM model needs to scan the entire XML document, then resolve generated an object tree, all tags and properties in the XML document are represented by objects instead of an isolated text. Because it is an object, there is a context that there is a relationship.

Basically, such DOC Element Note Textdoc refers to the entire tree object, including basic properties. ELEMENT refers to a label that can contain attributes and child node objects. Note is also a label, but you can't include sub-nodes and properties. Text is a simple text.

Since it is a tree operation, it is necessary to include the basic functions of nodes, deletion and browsing, etc. With these functions, you can do it completely to operate the XML document. Object-level operations are much easier than text operations directly, and it is necessary to be much more intuitive.

SAX model We said that the shortcomings of the DOM model are slow, and the memory is wasted. Because in many cases, we do not need to scan the entire XML document. And only certain information is needed, there is no need to generate a huge tree structure, especially when the file is very large. The SAX model is scanning and edge processing on the XML document. Similar to many programming ideas, SAX is an event-driven. This means that it can generate different messages during processing, and call the corresponding function to process. And you can quit a midway, this is very important. For example, in the process of downloading, you can download it while downloading, while parsing the lower part.

According to different situations, we use different methods for processing.

It should be noted that these models of XML are defined by standardized organizations, rather than a company specification. Therefore, the transplantation of the program should be more convenient.

XML programming example

Due to the comprehensive comparison of the documentation of Microft XML Parser, and we are more closely coupled with Visual C , we will use such a combination to give an exemplification for other languages.

One point for Microsoft XML Parser:

All objects are COM objects. To do this, we use SmartPointer to reduce addRef, ReleaseRef. Similarly, because the string unity is the BSTR type, the release of memory is also a more trouble of trouble, so use the _bstr_t class to encapsulate the BSTR data type, which can reduce unnecessary trouble. ..…to be continued ……

转载请注明原文地址:https://www.9cbs.com/read-2938.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.030, SQL: 9