C
Craig Daniel CA
Guest
I am trying to make a function that, given an MSXML2::IXMLDOMDocument*, will produce a new document that is formatted with newlines and indents. In an older version of my product, this worked perfectly well. After having ported everything to Visual Studio 2015, my formatting function no longer works. I have duplicated the problem in a small test program which simply opens a file and attempts to format it. I have pasted my formatting function below. One other thing that happens on VS2015 which did NOT happen under VS2010 is that I get the following two warnings.
warning C4192: automatically excluding ISequentialStream while importing type library msxml6.dll
warning C4192: automatically excluding _FILETIME while importing type library msxml6.dll
Anyone know why this doesnt work? What happens is, all formatting is removed. Even if the input document was formatted, the output wont be.
#define XML_DOM_DOCUMENT_PROG_ID L"Msxml2.FreeThreadedDOMDocument.6.0"
#define XMLWriterProgID L"Msxml2.MXXMLWriter.6.0"
#define SAXReaderProgID L"Msxml2.SAXXMLReader.6.0"
bool FormatDOMDocument(MSXML2::IXMLDOMDocument* pDoc)
{
CLSID clsid;
::CLSIDFromProgID(XML_DOM_DOCUMENT_PROG_ID, &clsid);
MSXML2::IXMLDOMDocument* newOutputDoc;
HRESULT hr = ::CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&newOutputDoc);
if (FAILED(hr))
return false;
newOutputDoc->async = false;
newOutputDoc->resolveExternals = false;
// Create the writer
MSXML2::IMXWriter* pMXWriter;
::CLSIDFromProgID(XMLWriterProgID, &clsid);
hr = ::CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pMXWriter));
if (FAILED(hr))
return false;
pMXWriter->omitXMLDeclaration = VARIANT_FALSE;
pMXWriter->standalone = VARIANT_TRUE;
pMXWriter->indent = VARIANT_TRUE;
pMXWriter->encoding = _bstr_t("UTF-8");
pMXWriter->output = CComVariant(newOutputDoc);
MSXML2::ISAXContentHandler* pISAXContentHandler;
if (FAILED(pMXWriter->QueryInterface(IID_PPV_ARGS(&pISAXContentHandler))))
return false;
MSXML2::ISAXErrorHandler* pISAXErrorHandler;
if (FAILED(pMXWriter->QueryInterface(IID_PPV_ARGS(&pISAXErrorHandler))))
return false;
// Create the SAX reader
MSXML2::ISAXXMLReader* pSAXReader;
::CLSIDFromProgID(SAXReaderProgID, &clsid);
hr = ::CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSAXReader));
if (FAILED(hr))
return false;
if (FAILED(pSAXReader->putContentHandler(pISAXContentHandler)) ||
FAILED(pSAXReader->putErrorHandler(pISAXErrorHandler)) ||
FAILED(pSAXReader->putProperty((WORD*)L"http://xml.org/sax/properties/lexical-handler", CComVariant(pMXWriter))) ||
FAILED(pSAXReader->putProperty((WORD*)L"http://xml.org/sax/properties/declaration-handler", CComVariant(pMXWriter)))
)
{
return false;
}
// Parse the input document while writing to the output
bool success = SUCCEEDED(pSAXReader->parse(CComVariant(pDoc)));
if (success)
{
CString sNewDoc = (char*)newOutputDoc->xml; // strings for debugging purposes
CString sOldDoc = (char*)pDoc->xml;
pDoc->documentElement = newOutputDoc->documentElement; // hand the new document over to the old XML object
}
newOutputDoc->Release();
return success;
}
Continue reading...
warning C4192: automatically excluding ISequentialStream while importing type library msxml6.dll
warning C4192: automatically excluding _FILETIME while importing type library msxml6.dll
Anyone know why this doesnt work? What happens is, all formatting is removed. Even if the input document was formatted, the output wont be.
#define XML_DOM_DOCUMENT_PROG_ID L"Msxml2.FreeThreadedDOMDocument.6.0"
#define XMLWriterProgID L"Msxml2.MXXMLWriter.6.0"
#define SAXReaderProgID L"Msxml2.SAXXMLReader.6.0"
bool FormatDOMDocument(MSXML2::IXMLDOMDocument* pDoc)
{
CLSID clsid;
::CLSIDFromProgID(XML_DOM_DOCUMENT_PROG_ID, &clsid);
MSXML2::IXMLDOMDocument* newOutputDoc;
HRESULT hr = ::CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&newOutputDoc);
if (FAILED(hr))
return false;
newOutputDoc->async = false;
newOutputDoc->resolveExternals = false;
// Create the writer
MSXML2::IMXWriter* pMXWriter;
::CLSIDFromProgID(XMLWriterProgID, &clsid);
hr = ::CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pMXWriter));
if (FAILED(hr))
return false;
pMXWriter->omitXMLDeclaration = VARIANT_FALSE;
pMXWriter->standalone = VARIANT_TRUE;
pMXWriter->indent = VARIANT_TRUE;
pMXWriter->encoding = _bstr_t("UTF-8");
pMXWriter->output = CComVariant(newOutputDoc);
MSXML2::ISAXContentHandler* pISAXContentHandler;
if (FAILED(pMXWriter->QueryInterface(IID_PPV_ARGS(&pISAXContentHandler))))
return false;
MSXML2::ISAXErrorHandler* pISAXErrorHandler;
if (FAILED(pMXWriter->QueryInterface(IID_PPV_ARGS(&pISAXErrorHandler))))
return false;
// Create the SAX reader
MSXML2::ISAXXMLReader* pSAXReader;
::CLSIDFromProgID(SAXReaderProgID, &clsid);
hr = ::CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSAXReader));
if (FAILED(hr))
return false;
if (FAILED(pSAXReader->putContentHandler(pISAXContentHandler)) ||
FAILED(pSAXReader->putErrorHandler(pISAXErrorHandler)) ||
FAILED(pSAXReader->putProperty((WORD*)L"http://xml.org/sax/properties/lexical-handler", CComVariant(pMXWriter))) ||
FAILED(pSAXReader->putProperty((WORD*)L"http://xml.org/sax/properties/declaration-handler", CComVariant(pMXWriter)))
)
{
return false;
}
// Parse the input document while writing to the output
bool success = SUCCEEDED(pSAXReader->parse(CComVariant(pDoc)));
if (success)
{
CString sNewDoc = (char*)newOutputDoc->xml; // strings for debugging purposes
CString sOldDoc = (char*)pDoc->xml;
pDoc->documentElement = newOutputDoc->documentElement; // hand the new document over to the old XML object
}
newOutputDoc->Release();
return success;
}
Continue reading...