Using MSXML to parse XML documents in VC + +

Source: Internet
Author: User
Tags add format define object format definition interface object model version
C++|xml

Iv. implementation of the procedure

The following is a concrete example to illustrate how to use MSXML to parse XML documents in VC + +.

(1) The source XML document (XMLFile.xml) is as follows:



Protection

We look for "Device" in the source document, set its Name property to test device, add the Model node to it, and set its text to "3".

(2) The source program is as follows:

CoInitialize (NULL);//Initialize COM.
CComPtr <IXMLDOMDocument> Spxmldom;
HRESULT hr=spxmldom.cocreateinstance (_uuidof (DOMDocument));
Creates a parser instance.
Variant_bool Bsuccess=false;
Hr=spxmldom->load (CComVariant (L "XMLFile.xml"), &bsuccess);
Loads an XML document.
CComBSTR Bstrss (L "Device");
CComPtr <IXMLDOMNode> Spdevice;
Hr=spxmldom->selectsinglenode (Bstrss,&spdevice); Search for "Device".
CComQIPtr <IXMLDOMElement> Spdev;
Spdev=spdevice;
Set the "Name" property of "Device".
hr= Spdev->setattribute (CComBSTR (L "name"), CComVariant ("test equipment"));
CComPtr <IXMLDOMNode> Spmodelnode; Hr=spxmldom->createnode (CComVariant (node_element), combstr
("Model"),null,& Spmodelnode); Create a "Model" node.
CComPtr <IXMLDOMNode> Spinsertednode;
Hr=spdevice->appendchild (Spmodelnode,&spinsertednode);
Add a new node under the "Device" node.
CString strid= "3";
Hr=spinsertednode->put_text (Strid.allocsysstring ());//Set the text of "Model".
Hr=spxmldom->save (CComVariant ("XMLFile.xml")); //Save the document.
CoUninitialize (); End the use of COM.

Because of the space reason, each step of the above code does not judge the type of HRESULT returned, nor does the exception capture processing, in the actual programming, the reader should make decision according to the returned HR to decide the process of the program, and should carry on the exception capture processing.

(3) The modified XML document is as follows


Protection
3

V. Concluding remarks

Because XML documents have more stringent syntax requirements than HTML, it is much easier to use and write an XML parser than to write an HTML parser. At the same time, because the XML document can not only mark the display property of the document, but also mark the structure of the document and the feature of the information, we can easily get the information of the specific node through the XML parser and display or modify it, which facilitates the user to operate and maintain the XML document.  At the same time, we need to note that XML is an open architecture and does not depend on any one company, so the development of xml-based applications will inevitably be supported by most software development platforms. I. Introduction to XML

The development of XML (xtensible Markup Language, or Extended Markup language) originates from 1996 years. At that time, the publishing giants and the web industry, after a joint discussion, proposed the creation of SGML (Standard generalized Markup Language, the standard Universal Identity language) subset (HML) recommendations. This subset is dedicated to the Web, extensible (or extensible), and leverages all the advanced features of the structured markup language, but rejects the complexities of SGML. Shortly after the first draft of the XML specification was released in November 1996, the first XML parser was published in January 1997. Then, the application of XML and the new specification and parser appear.

The HTML (hypertext Markup Language) language is a well-known markup language for developing Web pages, but it is not a programming language, and its main markup is a description of the structure of the document itself, because the machine itself cannot parse its contents. The XML language is a subset of the SGML language, which retains the primary use of SGML while significantly reducing the complexity of SGML. Not only can it represent the contents of a document, but it can also represent the structure of the document, which can be understood by the machine while being understood by the human. Although HTML is an application of SGML, XML is a subset of SGML, but XML is never formed by adding some elements to HTML, and there is a big difference between them. The biggest difference is that XML allows users to define their own elements for a particular purpose. In addition, another major difference is the problem that XML is working on, and it is the special problems that are encountered when using HTML.

XML requires strict standards that are more rigorous than HTML browsers for syntactic and structural requirements. The results of each parsing of an XML document are consistent, and HTML may be analyzed and displayed differently in different browsers. At the same time, the XML standard is a processing application of data, not just web pages, it can also involve the database e-business system, as well as any display system advanced applications. Developers can use XML to create custom data structures based on specific industrial requirements. These data structures and databases can be viewed on many devices without the need to use custom interfaces to view the same data on different display devices.

XML has three main elements: Document type declaration (dtd:document type declaration) or XML Schema (XML outline), Extensible Style Language (xsl:extensible Stylesheet Language) and extensible Link language (xlink:extensible link Language). The DTD and XML outlines define the logical structure of the XML file, defines the elements in the XML file, the attributes of the elements, and the relationship between elements and element attributes; Namespace (name Domain) realizes unified XML document data representation and data integration XSL is the language used to specify the style of the XML document, which makes the data independent of its presentation, such as XSL, which enables the Web browser to change the presentation of the document, such as changes in the order in which the data is displayed, and does not need to communicate with the server. By changing the style sheet, the same document can appear larger, or it can be folded to show only one layer of the outside, or it may become a printed format. And XLink will further expand the existing simple links on the web.

Second, Document Object Model (DOM)

Dom is the short name of document Object model, the application Interface (API) for application development and programming of Web documents. As a cross-platform, language-independent interface specification published by the Consortium, DOM provides standard program interfaces in different environments and applications that can be implemented in any language.

Dom uses object model and a series of interfaces to describe the content and structure of XML documents, that is, to use objects to model documents. The basic features of this object model implementation include:

· An interface that describes the presentation and operation of documents;

· The behavior and attributes of the interface;

· Interfaces and interoperation.

The DOM can parse the structured XML document, all content individuals in a document, such as directives, elements, entities, attributes, and so on, are represented by the object model, and the entire document is considered to be a structured information tree rather than a simple text stream, and the resulting object model is the node of the tree, which contains both methods and attributes. Therefore, all operations on the document are performed on the object tree. In the DOM, everything in the tree is an object, whether it is a root node or a property of an entity.

There are three main objects in the DOM:

· An XML Document object XML document is both an object and an entire XML document. It consists of a root element and a child element.

· An XML Node object XML node object represents a node within an XML document, such as an element, a comment, a namespace, and so on.

· The XML node list XML document Module list represents a collection of nodes.

With DOM, developers can dynamically create XML documents, traverse structures, add, modify, delete content, and so on. Its object-oriented characteristics, so that people in the process of XML parsing related transactions save a lot of energy, is a code to reuse the idea of a powerful programming tool.

Third, MSXML

Theoretically, according to the format definition of XML, we can write an XML parser ourselves, but in fact Microsoft has provided us with an XML parser, a dynamic link library called MSXML.DLL, which is actually a COM (Component Object Model) object Library, which encapsulates all the objects needed to parse the XML. Because COM is a language-independent reusable object that appears in binary format, you can invoke it in any language (such as vb,vc,delphi,c++ Builder or even script language, etc.) to implement parsing of XML documents in your application.

Msxml. The main COM interfaces that a DLL includes are:

1. DOMDocument

The DOMDocument object is the basis of the XML DOM, and you can use the properties and methods it exposes to browse, query, and modify the contents and structure of the XML document. DOMDocument represents the top-level node of the tree, implements all the basic methods of the DOM document, and provides additional member functions to support XSL and XSLT. It creates a document object, and all other objects can be obtained and created from this Document object.

2. IXMLDOMNode

IXMLDOMNode are basic objects in the Document Object Model (DOM), and elements, attributes, annotations, process directives, and other document components can be considered ixmldomnode. In fact, the DOMDocument object itself is also a IXMLDOMNode object.

3. IXMLDOMNodeList

IXMLDOMNodeList is actually a collection of node objects, and the additions, deletions, and changes of nodes can be reflected immediately in the collection, and all nodes can be traversed through the "for...next" structure.

4. Ixmldomparseerror

The Ixmldomparseerror interface is used to return detailed information that appears during parsing, including the error number, line number, character position, and text description.

In the concrete application can use the DOMDocument load method to load the XML document, use IXMLDOMNode selectnodes (the result of the query has multiple, get to store the search results of the list) or selectSingleNode (the result of the query has one, The first node found is returned in multiple cases to query, and the CreateNode and AppendChild methods are used to create nodes and append nodes, using the IXMLDOMElement SetAttribute and GetAttribute methods to set and get the properties of a node.



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.