写xml文件
1. //Create the XML
MSXML::IXMLDOMDocument2Ptr pXMLDoc;
HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument40));
Verify(pXMLDoc->loadXML(_T("")) == VARIANT_FALSE)));//为xml最顶层的结点
2. //Get the root element just created
MSXML::IXMLDOMElementPtr pXMLRootElem = pXMLDoc->GetdocumentElement();
//Add an attribute
pXMLRootElem->setAttribute(_T("Depth"),_variant_t(_T("0")));
3.//xml header
MSXML::IXMLDOMProcessingInstructionPtr pXMLProcessingNode = pXMLDoc->createProcessingInstruction("xml", " version='1.0' encoding='UTF-8'");
_variant_t vtObject;
vtObject.vt = VT_DISPATCH;
vtObject.pdispVal = pXMLRootElem;
vtObject.pdispVal->AddRef();
pXMLDoc->insertBefore(pXMLProcessingNode,vtObject);
4.//Create the child elements and set the attributes
MSXML::IXMLDOMElementPtr pXMLChild1 = pXMLDoc->createElement(_T("Child1")); //Create first child element
pXMLChild1->setAttribute(_T("Depth"),_T("1"));
pXMLChild1->Puttext(_T("This is a child of Parent")); //Set the element value
pXMLChild1 = pXMLRootElem->appendChild(pXMLChild1);
MSXML::IXMLDOMElementPtr pXMLChild2 = pXMLDoc->createElement(_T("Child2"));
pXMLChild2->setAttribute(_T("Depth"), _T("1"));
pXMLChild2 = pXMLRootElem->appendChild(pXMLChild2); //Child2 is a sibling of Child1
MSXML::IXMLDOMElementPtr pXMLChild3 = pXMLDoc->createElement(_T("Child3"));
pXMLChild3->setAttribute(_T("Depth"), _T("2"));
pXMLChild3 = pXMLChild2->appendChild(pXMLChild3); //Child3 is a direct child of Child2
MSXML::IXMLDOMElementPtr pXMLChild4 = pXMLDoc->createElement(_T("Child4"));
pXMLChild4->setAttribute(_T("Depth"), _T("3"));
pXMLChild4->Puttext(_T("This is a child of Child3"));
pXMLChild4 = pXMLChild3->appendChild(pXMLChild4); //Child4 is a direct child of Child3
4.// Format the XML. This requires a style sheet
MSXML::IXMLDOMDocument2Ptr loadXML;
hr = loadXML.CreateInstance(__uuidof(DOMDocument40));
if(FAILED(hr))
{
ShowError(loadXML);
return;
}
//We need to load the style sheet which will be used to indent the XMl properly.
if(loadXML->load(variant_t(_T("StyleSheet.xsl"))) == VARIANT_FALSE)
{
ShowError(loadXML);
return;
}
//Create the final document which will be indented properly
MSXML::IXMLDOMDocument2Ptr pXMLFormattedDoc;
hr = pXMLFormattedDoc.CreateInstance(__uuidof(DOMDocument40));
CComPtr pDispatch;
hr = pXMLFormattedDoc->QueryInterface(IID_IDispatch, (void**)&pDispatch);
if(FAILED(hr))
{
return;
}
_variant_t vtOutObject;
vtOutObject.vt = VT_DISPATCH;
vtOutObject.pdispVal = pDispatch;
vtOutObject.pdispVal->AddRef();
//Apply the transformation to format the final document
hr = pXMLDoc->transformNodeToObject(loadXML,vtOutObject);
//By default it is writing the encoding = UTF-16. Let us change the encoding to UTF-8
MSXML::IXMLDOMNodePtr pXMLFirstChild = pXMLFormattedDoc->GetfirstChild(); //
MSXML::IXMLDOMNamedNodeMapPtr pXMLAttributeMap = pXMLFirstChild->Getattributes(); // A map of the a attributes (vesrsion, encoding) values (1.0, UTF-8) pair
MSXML::IXMLDOMNodePtr pXMLEncodNode = pXMLAttributeMap->getNamedItem(_T("encoding"));
pXMLEncodNode->PutnodeValue(_T("UTF-8")); //encoding = UTF-8
5.//save
if(sLocation.IsEmpty()) //User forgot to set the lcoation?
sLocation = _T("Javed.xml");
hr = pXMLFormattedDoc->save(sLocation.AllocSysString());
用msxml写xml文件
最新推荐文章于 2025-04-13 13:42:12 发布