0

MSXML DLL:msxml3.dll

我正在使用 MSXML DOM 编写 XML,并希望添加多个处理指令。前任:

第一条处理指令。

xml version="1.0" encoding="ISO-8859-1"?

二次加工指令

xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?

我使用 MSXML DOM 编写了以下代码。但它未能添加第一和第二个处理指令。

代码开始

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);

请帮助理解为什么 MSXML 不添加多个处理指令?

4

1 回答 1

1

我找到了解决方案:

//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());
于 2013-02-28T07:22:58.190 回答