XML programming example (2)

zhaozj2021-02-08  242

XML programming example (2)

DOM model: The above has been said, for the DOM model, the entire XML file is parsed into a tree structure. All labels, attributes, etc. are treated as objects. Therefore, you must understand the meaning of the clear object, and its interrelationship, and can operate correctly. In order to have a sense of sensibility, let's get started. (For the convenience of explanation, there is basically no abnormality in the code)

Generate an XML text

Suppose we want to create a xml text -11 9

We should first create a Document object first, as follows: msxml :: ixmldomdocumentptr pdoc; pdoc.createInstance (__ uuidof (msxml :: domdocument);

If you create success, then we will get an XMLDOMDocument object instance. The next step is to add root node documentElement, remember that XML has only one root.

Create a Element object as a root node msxml :: ixmldomelementptr pDoCELEMENT = PDOC-> CreateElement ("China");

Insert the root node into the directory tree pdoc-> appendchild (pdocelement);

Ok, build a tree root, let's take a look at the results: use PDOC -> XML to get the text of the entire DOM object because there is no thing under the root, so it only shows

Now, we have to insert a child node under the root and set the node text (Text) msxml :: ixmldomelementptr pnewchildelement; pnewchildelement = pdoc-> createElement ("beijing"); pnewchildElement-> Puttext ("- 11"); pdocelement- > appendchild (pnewchildElement);

At this time, the entire XML text should be -11

Add a child node, and set the node attribute (Attribute) pNewChildElement = pDoc-> createElement ( "Shanghai"); pNewChildElement-> Puttext ( "9"); pNewChildElement-> setAttribute ( "Weather", _ variant_t ( "Cloudy") PDoCELEMENT-> appendchild (pnewchildelement);

So, we can get the expected XML text. Other operations:

Delete Action: Delete the child node PDoCELEMENT-> RemoveChild (PNEWCHILDELEMENT) storage operation: pdoc-> save ();

Load existing XML text

If we already have an XML file, you want to parse it, you can use the document or loadXml of the Document object to load, and perform syntax analysis while loading.

If the load is successful, a tree structure is generated in memory. With the DOM model, we can make a variety of operations. The most common thing is that we need to find specific information and processed. Find Location Use SelectsingLenode (XPATH), SelectNodes (XPath) to locate the label to obtain the corresponding Node (s) object. XPath XPath is a string similar to the file path name, as well as the SQL query statement, can qualify the search range. Find the specified object, we can process, add, delete, value, and more. Summary: The above is only a simple introduction to the DOM model, in order to get started quickly. To learn more, you must also check the documentation of the SDK. If possible, the SAX model will be described later, XSLT conversion to XML, etc.

Related links: To use MSXML Parser, you must download its SDK and run libraries first. Http://download.microsoft.com/download/xml/install/3.0/win98me/en-us/msxml3.exehtp://download.microsoft.com/download/xml/sdk/3.0/win98me/en-us/ Xmlsdk.exe

// ------------- below is the code example -----------

#include "stdafx.h" #include "iostream.h" #include "msxml.h" #include "atlbase.h" #import "msxml.dll" // introduced type library

#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF

Int exit (); void loadfromstring (); void createXML ();

// XML text template _bstr_t xmltemple = " -11 9 ";

INT main () {cout << "XML programming - demonstration program" << endl;

Coinitialize (NULL); // Initialization COM environment

Cout << "------ Generate a new XML text ------" << end1;

Createxml ();

Cout << "------ Read existing XML text ------" << end1

LoadFromstring (); returnif;

} void createXML () {msxml :: ixmldomdocumentptr pdoc; hResult hr = pdoc.createInstance (__ uuidof (msxml :: domdocument)); if (! successted (hr)) {

Cout << "Unable to create a DomDocument object, please check if the MS XML Parser running is installed!" << endl; exit ();

}

Msxml :: ixmldomelementptr pDoCELEMENT = PDOC-> CreateElement ("China"); pdoc-> appendchild (pdocelement);

Cout << "Generated Tree Root: / N" << PDOC-> XML << ENDL;

Msxml :: ixmldomelementptr pnewchildelement;

PnewchildElement = pdoc-> CreateElement ("beijing");

PNewchildElement-> PutText ("- 11");

PDOCELEMENT-> appendchild (pnewchildElement);

Cout << "Add Node: / N" << PDOC-> XML << ENDL;

PNewchildElement = PDOC-> CreateElement ("Shanghai);

PNewchildElement-> PutText ("9");

PnewchildElement-> SetAttribute ("Weather", _ variant_t ("cloudy"));

PDOCELEMENT-> appendchild (pnewchildElement);

Cout << "Add Node: / n" << PDOC-> XML << ENDL;

PDOCELEMENT-> RemoveChild (pnewchildElement);

Cout << "Remove the node just added: / n" << pdoc-> XML << ENDL;

}

Void loadingfromstring () {

Msxml :: ixmldomdocumentptr pdoc;

HRESULT HR = PDoc.createInstance (__ uuidof (msxml :: domdocument);

IF (! succeeded (hr))

{Cout << "Unable to create a DomDocument object, please check if the MS XML Parser running is installed!" << Endl; exit ();

}

PDOC-> LoadXML (XMLTemple);

Cout << "Read Result: / N" << PDOC-> XML << ENDL;

Msxml :: ixmldomelementptr pDoCELEMENT = PDOC-> getDocumentElement ();

MSXML :: ixmldomelementptr pelement = pdocelement-> selectsinglenode ("shanghai");

PDoCELEMENT-> RemoveChild (pelement);

Cout << "Location Delete Shanghai Node: / N" << PDOC-> XML << ENDL;

Cout << "Save results Save ..... (simulation)" << endl;

Cout << "/ n is good, it is so simple" << endl;}

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

New Post(0)