BOOL CXMLDemoApp::InitInstance()
{
// {
if (!AfxOleInit())
{
AfxMessageBox(_T("Failed to initialise OLE libraries"));
return FALSE;
}
// }
AfxEnableControlContainer();
...
}
void CXMLDemoDlg::OnBnClickedCreatexml()
{
// TODO: Add your control notification handler code here
//Create the XML
MSXML2::IXMLDOMDocument2Ptr pXMLDoc;
HRESULT hr = pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
if(FAILED(hr))
{
AfxMessageBox(_T("Failed to create the XML class instance"));
return;
}
if(pXMLDoc->loadXML(_T("<Parent></Parent>")) == VARIANT_FALSE)
{
ShowError(pXMLDoc);
return;
}
//Get the root element just created
MSXML2::IXMLDOMElementPtr pXMLRootElem = pXMLDoc->GetdocumentElement();
//Add an attribute
pXMLRootElem->setAttribute(_T("Depth"),_variant_t(_T("0")));
//Create the child elements and set the attributes
MSXML2::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);
MSXML2::IXMLDOMElementPtr pXMLChild2 = pXMLDoc->createElement(_T("Child2"));
pXMLChild2->setAttribute(_T("Depth"), _T("1"));
pXMLChild2 = pXMLRootElem->appendChild(pXMLChild2); //Child2 is a sibling of Child1
MSXML2::IXMLDOMElementPtr pXMLChild3 = pXMLDoc->createElement(_T("Child3"));
pXMLChild3->setAttribute(_T("Depth"), _T("2"));
pXMLChild3 = pXMLChild2->appendChild(pXMLChild3); //Child3 is a direct child of Child2
MSXML2::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
// Format the XML. This requires a style sheet
MSXML2::IXMLDOMDocument2Ptr loadXML;
hr = loadXML.CreateInstance(__uuidof(MSXML2::DOMDocument));
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
MSXML2::IXMLDOMDocument2Ptr pXMLFormattedDoc;
hr = pXMLFormattedDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
//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
MSXML2::IXMLDOMNodePtr pXMLFirstChild = pXMLFormattedDoc->GetfirstChild(); // <?xml version="1.0" encoding="UTF-8"?>
MSXML2::IXMLDOMNamedNodeMapPtr pXMLAttributeMap = pXMLFirstChild->Getattributes(); // A map of the a attributes (vesrsion, encoding) values (1.0, UTF-8) pair
MSXML2::IXMLDOMNodePtr pXMLEncodNode = pXMLAttributeMap->getNamedItem(_T("encoding"));
pXMLEncodNode->PutnodeValue(_T("UTF-8")); //encoding = UTF-8
UpdateData(); //Get the location
if(m_sLocation.IsEmpty()) //User forgot to set the lcoation?
m_sLocation = _T("Javed.xml");
hr = pXMLFormattedDoc->save(m_sLocation.AllocSysString());
if(FAILED(hr))
{
ShowError(pXMLFormattedDoc);
return;
}
m_sLocation += _T(" created");
AfxMessageBox(m_sLocation);
}
// Check for valid XML element
if (lpXMLElement != NULL)
{
...
// Check for XML element type
if (lpXMLElement->GetElementType() == XET_TAG)
{
// XML element attribute collection
CXMLElement* lpXMLChildElement = lpXMLElement->GetFirstChild();
while ((lpXMLChildElement != NULL) && (lpXMLChildElement->GetElementType() == XET_ATTRIBUTE))
{
...
lpXMLChildElement = lpXMLElement->GetNextChild();
}
// XML element child collection
while(lpXMLChildElement != NULL)
{
// XML element
bResult = BrowseXMLElement(lpXMLChildElement);