用MSXML 4.0:
1)用一般的指针
2)用智能指针,比较简单
下面的例子用智能指针:
 步骤:        
Programmatically, the dynamDOMsmart application performs the following steps:
    - Creates an XML DOM instance (pXMLDom).
- Calls the createProcessInstructionmethod onpXMLDom. This creates a processing instruction node (pi) targeted for XML 1.0.
- Calls the appendChildmethod onpXMLDom. This adds the processing instruction node (pi) topXMLDom.
- Calls the createCommentmethod on the DOM object (pXMLDom) to create a comment node (pc) and then append itpXMLDom.
- Creates a <root>element as the document element, with acreatedattribute whose value is set to a string value of"using DOM". Adds this element(<root>) to the DOM object (pXMLDom).
- Creates a <node1>element with some character data as its content. Appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).
- Creates a <node2>element that contains a CDATA section (pcd) with markup text. Appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).
- Creates a <node3>element that contains a DOM document fragment (pdf). This fragment contains three other empty child elements:<subNode1>,<subNode2>, and<subNode3>. The code then appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).
- Saves this dynamically created DOM object to the project's main directory, and prints the XML data in the application console. 
源代码:
 #include <stdio.h>
#include <stdio.h>
 #import <msxml4.dll>
#import <msxml4.dll>
 using namespace MSXML2;
using namespace MSXML2;
 int main(int argc, char* argv[])
int main(int argc, char* argv[])


 {
{
 IXMLDOMDocument3Ptr pXMLDom;
IXMLDOMDocument3Ptr pXMLDom;
 HRESULT hr;
HRESULT hr;
 CoInitialize(NULL);
CoInitialize(NULL);
 hr = pXMLDom.CreateInstance(__uuidof(DOMDocument40));
hr = pXMLDom.CreateInstance(__uuidof(DOMDocument40));
 if (FAILED(hr))
if (FAILED(hr))


 {
{
 printf("Failed to CreateInstance on an XML DOM");
printf("Failed to CreateInstance on an XML DOM");
 return NULL;
return NULL;
 }
}
 pXMLDom->preserveWhiteSpace = VARIANT_TRUE;
pXMLDom->preserveWhiteSpace = VARIANT_TRUE;
 // Create a processing instruction targeted for xml.
// Create a processing instruction targeted for xml.
 IXMLDOMProcessingInstructionPtr pi;
IXMLDOMProcessingInstructionPtr pi;
 pi = pXMLDom->createProcessingInstruction("xml", "version='1.0'");
pi = pXMLDom->createProcessingInstruction("xml", "version='1.0'");

 if (pi != NULL)
if (pi != NULL)  {
{
 pXMLDom->appendChild(pi);
pXMLDom->appendChild(pi);
 pi.Release();
pi.Release();
 }
}
 // Create a processing instruction targeted for xml-stylesheet.
// Create a processing instruction targeted for xml-stylesheet.
 pi = pXMLDom->createProcessingInstruction("xml-stylesheet",
pi = pXMLDom->createProcessingInstruction("xml-stylesheet",
 "type='text/xml' href='dom.xsl'");
"type='text/xml' href='dom.xsl'");

 if (pi != NULL)
if (pi != NULL)  {
{
 pXMLDom->appendChild(pi);
pXMLDom->appendChild(pi);
 pi.Release();
pi.Release();
 }
}
 // Create a comment for the document.
// Create a comment for the document.
 IXMLDOMCommentPtr pc;
IXMLDOMCommentPtr pc;
 pc = pXMLDom->createComment("sample xml file created using XML DOM object.");
pc = pXMLDom->createComment("sample xml file created using XML DOM object.");

 if (pc != NULL)
if (pc != NULL)  {
{
 pXMLDom->appendChild(pc);
pXMLDom->appendChild(pc);
 pc.Release();
pc.Release();
 }
}
 // Create the root element (i.e., the documentElement).
// Create the root element (i.e., the documentElement).
 IXMLDOMElementPtr pe;
IXMLDOMElementPtr pe;
 pe = pXMLDom->createElement("root");
pe = pXMLDom->createElement("root");
 // Create a "created" attribute for the root element and
// Create a "created" attribute for the root element and
 // assign the "using dom" character data as the attribute value.
// assign the "using dom" character data as the attribute value.
 IXMLDOMAttributePtr pa;
IXMLDOMAttributePtr pa;
 pa = pXMLDom->createAttribute("created");
pa = pXMLDom->createAttribute("created");
 if (pa != NULL)
if (pa != NULL)


 {
{
 pa->value = "using dom";
pa->value = "using dom";
 pe->setAttributeNode(pa);
pe->setAttributeNode(pa);
 pa.Release();
pa.Release();
 }
}
 // Add the root element to the DOM instance.
// Add the root element to the DOM instance.
 pXMLDom->appendChild(pe);
pXMLDom->appendChild(pe);
 pe.Release();
pe.Release();
 // Next, we will create and add more nodes to the root element
// Next, we will create and add more nodes to the root element
 // we've just created.
// we've just created.
 // Create an element to hold text content.
// Create an element to hold text content.
 pe = pXMLDom->createElement("node1");
pe = pXMLDom->createElement("node1");
 if (pe != NULL)
if (pe != NULL)


 {
{
 // Add newline + tab for indentation.
// Add newline + tab for indentation.
 pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
 pe->text = "some character data";
pe->text = "some character data";
 pXMLDom->documentElement->appendChild(pe);
pXMLDom->documentElement->appendChild(pe);
 pe.Release();
pe.Release();
 }
}
 // Create an element to hold a CDATA section.
// Create an element to hold a CDATA section.
 pe=pXMLDom->createElement("node2");
pe=pXMLDom->createElement("node2");
 if (pe != NULL)
if (pe != NULL)


 {
{
 // Add newline + tab for indentation.
// Add newline + tab for indentation.
 pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
 IXMLDOMCDATASectionPtr pcd;
IXMLDOMCDATASectionPtr pcd;
 pcd = pXMLDom->createCDATASection("<some mark-up text>");
pcd = pXMLDom->createCDATASection("<some mark-up text>");

 if (pcd != NULL)
if (pcd != NULL)  {
{
 pe->appendChild(pcd);
pe->appendChild(pcd);
 pcd.Release();
pcd.Release();
 }
}
 pXMLDom->documentElement->appendChild(pe);
pXMLDom->documentElement->appendChild(pe);
 pe.Release();
pe.Release();
 }
}
 // Create an element to hold three empty subelements.
// Create an element to hold three empty subelements.
 pe=pXMLDom->createElement("node3");
pe=pXMLDom->createElement("node3");
 if (pe != NULL)
if (pe != NULL)


 {
{
 // Add newline +tab for indentation.
// Add newline +tab for indentation.
 pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
 IXMLDOMDocumentFragmentPtr pdf;
IXMLDOMDocumentFragmentPtr pdf;
 pdf = pXMLDom->createDocumentFragment();
pdf = pXMLDom->createDocumentFragment();
 pdf->appendChild(pXMLDom->createTextNode("\n\t\t"));
pdf->appendChild(pXMLDom->createTextNode("\n\t\t"));
 pdf->appendChild(pXMLDom->createElement("subNode1"));
pdf->appendChild(pXMLDom->createElement("subNode1"));
 pdf->appendChild(pXMLDom->createTextNode("\n\t\t"));
pdf->appendChild(pXMLDom->createTextNode("\n\t\t"));
 pdf->appendChild(pXMLDom->createElement("subNode2"));
pdf->appendChild(pXMLDom->createElement("subNode2"));
 pdf->appendChild(pXMLDom->createTextNode("\n\t\t"));
pdf->appendChild(pXMLDom->createTextNode("\n\t\t"));
 pdf->appendChild(pXMLDom->createElement("subNode3"));
pdf->appendChild(pXMLDom->createElement("subNode3"));
 pdf->appendChild(pXMLDom->createTextNode("\n\t"));
pdf->appendChild(pXMLDom->createTextNode("\n\t"));
 pe->appendChild(pdf);
pe->appendChild(pdf);
 pdf.Release();
pdf.Release();
 pXMLDom->documentElement->appendChild(pe);
pXMLDom->documentElement->appendChild(pe);
 pe.Release();
pe.Release();
 pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n"));
pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n"));
 }
}
 printf("Dynamically created DOM:\n%s\n",
printf("Dynamically created DOM:\n%s\n",
 (LPCSTR)pXMLDom->xml);
(LPCSTR)pXMLDom->xml);
 hr = pXMLDom->save("dynaDom.xml");
hr = pXMLDom->save("dynaDom.xml");
 if (FAILED(hr))
if (FAILED(hr))


 {
{
 printf("Failed to save DOM to dynaDom.xml\n");
printf("Failed to save DOM to dynaDom.xml\n");
 }
}
 else
else


 {
{
 printf("DOM saved to dynamDom.xml\n");
printf("DOM saved to dynamDom.xml\n");
 }
}
 if (pXMLDom) pXMLDom.Release();
if (pXMLDom) pXMLDom.Release();
 CoUninitialize();
CoUninitialize();
 return 0;
return 0;
 }
}

结果:
Dynamically created DOM:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xml' href='dom.xsl'?>
<!--sample xml file created using XML DOM object.-->
<root created="using dom">
<node1>some character data</node1>
<node2><![CDATA[<some mark-up text>]]></node2>
<node3>
<subNode1/>
<subNode2/>
<subNode3/>
</node3>
</root>
DOM saved to dynamDom.xml
-----------------------------
在此过程中,经常会有一些错误:保存文件的路径,有的时候我写绝对路径,但是结果它却还是保存到相对路径,(为什么那?)还有里面的字符格式的转化,比较复杂,哈哈!欢迎大家来讨论:
哪位高手知道,关于保存路径的具体的东西啊,反正我发现相对路径有的时候并不总是相对你的原程序,当你打开文件处理在保存时,相对路径是相对你程序打开的文件的路径!
还有其他的吗,该轮到你们拉: