Reputation: 33
MSXML DLL :msxml3.dll
I am writing XML using MSXML DOM & expected to add multiple processing instruction. ex:
first processing instruction.
xml version="1.0" encoding="ISO-8859-1"?
second processing instruction
xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?
I have write below code using MSXML DOM. But it failed to add first & second processing instruction.
Code start
MSXML2::IXMLDOMElementPtr pXMLRootElem;
MSXML2::IXMLDOMNodePtr pTestDOMNodePtr;
//Create an instance of the DOMDocument object:
m_docPtr.CreateInstance(__uuidof(MSXML2::DOMDocument30));
char* xmlfile = (char*)xmlFileFullPath.c_str();
_variant_t varXml(xmlfile); //XML file to load
m_docPtr->async = VARIANT_FALSE;
m_docPtr->validateOnParse = VARIANT_FALSE;
m_docPtr->resolveExternals = VARIANT_FALSE;
//load XML file.
if(m_docPtr->loadXML(_T("<catalog><cd></cd></catalog>")) == VARIANT_FALSE)
{
CCommonFunction::log ("Failed to create the XML file.");
return false;
}
//Get the root element just created
pXMLRootElem = m_docPtr->GetdocumentElement();
//// Add first ProcessingInstruction <?xml version="1.0" encoding="ISO-8859-1"?>
MSXML2::IXMLDOMProcessingInstructionPtr pXMLProcessingNode =
m_docPtr->createProcessingInstruction("xml", " version='1.0' encoding='UTF-8'");
_variant_t vtObject;
vtObject.vt = VT_DISPATCH;
vtObject.pdispVal = pXMLRootElem;
vtObject.pdispVal->AddRef();
m_docPtr->insertBefore(pXMLProcessingNode,vtObject);
//// Add second ProcessingInstruction ?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
MSXML2::IXMLDOMProcessingInstructionPtr pXSLTNode =
m_docPtr->createProcessingInstruction("xml-stylesheet", "type='text/xsl' href='cdcatalog.xsl'");
m_docPtr->insertBefore(pXSLTNode,vtObject);
Please help in understanding why MSXML is not adding multiple processing instruction ?
Upvotes: 0
Views: 1638
Reputation: 33
I found the solution:
//Add header with XML version 1.0
MSXML2::IXMLDOMProcessingInstructionPtr ptrPI = m_docPtr->createProcessingInstruction(L"xml", L"version='1.0' encoding='UTF-8'");
m_docPtr->insertBefore(ptrPI, m_docPtr->documentElement.GetInterfacePtr());
//Add header with XML stylesheet
MSXML2::IXMLDOMProcessingInstructionPtr ptrPI1 = m_docPtr->createProcessingInstruction(L"xml-stylesheet", L"type='text/xsl' href='test1.xsl");
m_docPtr->insertBefore(ptrPI1, m_docPtr->documentElement.GetInterfacePtr());
Upvotes: 1