VC Getting Started Time (XML)

zhaozj2021-02-16  51

xml

He Zhidan

We build an XML file, as shown in the figure:

First, establish a dialog-based program, the project is called XML;

Second, initialize OL.

Bool cxmlapp :: initInstance ()

{

Afxoleinit ();

.......

}

Third, add a button in the dialog, ID is IDC_CREATE, and we generate an XML file in this button's response function.

Void cxmldlg :: oncreate ()

{

Msxml2 :: ixmldomdocumentptr pdoc;

Msxml2 :: ixmldomelementptr xmlroot;

// Create a DomDocument object

HRESULT HR = PDoc.createInstance (__ uuidof (msxml2 :: domdocument30));

IF (! succeeded (hr))

{

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

}

// Name of the root node is China

PDOC-> Raw_createElement ((_ BSTR_T) (char *) "China", & XMLROOT);

PDOC-> Raw_AppendChild (XMLROOT, NULL);

Msxml2 :: ixmldomelementptr childNode;

PDOC-> Raw_createElement ((_ bstr_t) (char *) "city", & childnode);

ChildNode-> Puttext ("wuhan"); // node value

ChildNode-> SetAttribute ("Population", "8,000,000"); // Property name, attribute value

ChildNode-> SetAttribute ("Area", "10000");

XMLROOT-> appendchild (childnode);

PDOC-> Raw_createElement ((_ bstr_t) (char *) "city", & childnode);

ChildNode-> Puttext ("Shanghai");

ChildNode-> SetAttribute ("Population", "12,000,000");

ChildNode-> SetAttribute ("Area", "12000");

XMLROOT-> appendchild (childnode);

/ / Save to the file

// If there is no existence, it is created.

PDOC-> Save ("f: //he.xml");

}

Don't forget #import "msxml4.dll" // introduced type library

4. Add a button, ID is IDC_GET, read the XML file in the response function of this button.

Void cxmldlg :: ONGET ()

{

// Create a DomDocument object

Msxml2 :: ixmldomdocumentptr pdoc;

HRESULT HR = PDoc.createInstance (__ uuidof (msxml2 :: domdocument30));

IF (! succeeded (hr))

{

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

}

// Load file

PDOC-> LOAD ("f: //he.xml");

// Find a node called City, "//" is shown in any layer.

MSXML2 :: ixmldomelementptr childNode; childNode = (msxml2 :: ixmldomelementptr) (pdoc-> selectsinglenode ("// city");

// Get a node type

MSXML2 :: Domnodetype NodetyPE;

ChildNode-> Get_NodetyPE (& nodetype);

// node name

BSTR var;

CString name;

ChildNode-> Get_Nodename (& var);

Name = (char *) (_ BSTR_T) VAR;

// node value

Variant Varval;

ChildNode-> Get_NodetyPedValue (& VARVAL);

CString strval = (char *) (_ BSTR_T) VARVAL;

// Node property, put it in the list

Msxml2 :: ixmldomnamednodemapptr patts = null;

Msxml2 :: ixmldomnodeptr pattritem;

ChildNode-> Get_attributes;

Long ncount;

PATTRS-> GET_LENGTH (& NCOUNT);

For (int i = 0; i

{

PATTRS-> GET_ITEM (I, & PatTritem);

/ / We can get the property name and attribute value through the function get_nodename, get_nodetypedValue.

/ / Can also be directly obtained

CString StrattrName = (char *) (_ BSTR_T) Pattritem-> Nodename;

CString StrattrValue = (char *) (_ bstr_t) Pattritem-> NodetypedPalue;

}

}

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

New Post(0)