Beispiel #1
0
/******************************************************************************
Function Name  :  FormatDOMDocument
Input(s)       :  MSXML2::IXMLDOMDocumentPtr pDoc
                  CString& omstrFilePath
Output         :  bool
Functionality  :  Formats the pDoc Pointer into File omStrFilePath
Member of      :  CTestSetupEntity
Friend of      :  -
Author(s)      :  Venkatanarayana Makam, GT-Derka
Date Created   :  06/04/2011
Modifications  :
******************************************************************************/
BOOL CTestSetupEntity::FormatDOMDocument(MSXML2::IXMLDOMDocumentPtr pDoc, CString& omstrFilePath)
{
    //Referance Taken From Msdn
    MSXML2::ISAXXMLReaderPtr pSaxXmlReader = nullptr;

    pSaxXmlReader.CreateInstance(L"Msxml2.SAXXMLReader");


    CComPtr<IStream> pStream;
    DWORD grfMode = STGM_WRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE;

    //W4 Removal
    if(SHCreateStreamOnFile ((LPCTSTR)omstrFilePath, grfMode, &pStream) == S_OK)
    {
        MSXML2::IMXWriterPtr pImxWriter;

        pImxWriter.CreateInstance (L"Msxml2.MXXMLWriter");
        pImxWriter->put_encoding(L"UTF-8"); // Create UTF-8-encoded XML-File instead of UTF-16 */
        pImxWriter->put_output (CComVariant(pStream));

        pSaxXmlReader->putContentHandler((MSXML2::ISAXContentHandlerPtr)pImxWriter);
        pSaxXmlReader->putErrorHandler((MSXML2::ISAXErrorHandlerPtr)pImxWriter);
        pSaxXmlReader->putDTDHandler((MSXML2::ISAXDTDHandlerPtr)pImxWriter);

        pImxWriter->put_byteOrderMark(VARIANT_TRUE);
        pImxWriter->put_indent(VARIANT_TRUE);

        //W4 Removal
        pSaxXmlReader->parse((_variant_t)(pDoc.GetInterfacePtr()));

        pImxWriter->flush ();
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
Beispiel #2
0
	//-------------------------------------------------------------------------
	// Function Name    :SaveWithFormatted
	// Parameter(s)     :LPCTSTR lpszFilePath	[in, optional] 
	//					:LPCTSTR lpszEncoding	[in, optional] 
	// Return           :BOOL
	// Memo             :Save the xml file in formatted
	// History			:V3.1 Thanks roel_'s advice to support formatted output
	//-------------------------------------------------------------------------
	BOOL CXml::SaveWithFormatted(LPCTSTR lpszFilePath /* = NULL */, LPCTSTR lpszEncoding /* = _T("UTF-8") */)
	{
		if( m_pDoc == NULL )
		{
			ASSERT(FALSE);
			return FALSE;
		}

		HRESULT hr = S_OK;

		MSXML2::IMXWriterPtr pMXWriter = NULL;
		MSXML2::ISAXXMLReaderPtr pSAXReader = NULL;
		MSXML2::ISAXContentHandlerPtr pISAXContentHandler = NULL;
		MSXML2::ISAXErrorHandlerPtr pISAXErrorHandler = NULL;
		MSXML2::ISAXDTDHandlerPtr pISAXDTDHandler = NULL;
		try
		{
		
			// create
			hr = pMXWriter.CreateInstance( __uuidof(MSXML2::MXXMLWriter));
			if( FAILED(hr) ){ ASSERT(FALSE); goto lblErrorHandlerForSave; }
			
			hr = pSAXReader.CreateInstance( __uuidof(MSXML2::SAXXMLReader));
			if( FAILED(hr) ){ ASSERT(FALSE); goto lblErrorHandlerForSave; }

			// save in formatted
			pISAXContentHandler = pMXWriter;
			pISAXErrorHandler = pMXWriter;
			pISAXDTDHandler = pMXWriter;

			if (FAILED (pMXWriter->put_omitXMLDeclaration (VARIANT_FALSE)) ||
				FAILED (pMXWriter->put_standalone (VARIANT_FALSE)) ||
				FAILED (pMXWriter->put_indent (VARIANT_TRUE)) ||
				FAILED (pMXWriter->put_encoding(_bstr_t(lpszEncoding)) )
				)
			{
				ASSERT(FALSE);
				goto lblErrorHandlerForSave;
			}

			if (FAILED(pSAXReader->putContentHandler (pISAXContentHandler)) ||
				FAILED(pSAXReader->putDTDHandler (pISAXDTDHandler)) ||
				FAILED(pSAXReader->putErrorHandler (pISAXErrorHandler)) ||
				FAILED(pSAXReader->putProperty ((unsigned short*)L"http://xml.org/sax/properties/lexical-handler", _variant_t(pMXWriter.GetInterfacePtr()))) ||
				FAILED(pSAXReader->putProperty ((unsigned short*)L"http://xml.org/sax/properties/declaration-handler", _variant_t(pMXWriter.GetInterfacePtr()))))
			{
				ASSERT(FALSE);
				goto lblErrorHandlerForSave;
			}
						
			IStream * pOutStream = NULL;
			::CreateStreamOnHGlobal( NULL, TRUE, &pOutStream);
			hr = pMXWriter->put_output(_variant_t(pOutStream));
			if( FAILED(hr) ){ ASSERT(FALSE); goto lblErrorHandlerForSave; }

			hr = pSAXReader->parse(m_pDoc.GetInterfacePtr());
			if( FAILED(hr) ){ ASSERT(FALSE); goto lblErrorHandlerForSave; }
					
			if( lpszFilePath == NULL )
			{
				ASSERT(!m_strFilePath.IsEmpty());
				if( !CXml::SaveStreamToFile( pOutStream, m_strFilePath) )
					return FALSE;
			}
			else
			{
				if( !CXml::SaveStreamToFile( pOutStream, lpszFilePath) )
					return FALSE;
				m_strFilePath = lpszFilePath;
			}			
		}
		catch ( _com_error e )
		{
			TRACE( _T("CXml::SaveWithFormatted( %s ) failed:%s\n"), lpszFilePath, e.ErrorMessage());
			ASSERT( FALSE );
			hr = e.Error();
		}

lblErrorHandlerForSave:
		RELEASE_PTR(pISAXDTDHandler);
		RELEASE_PTR(pISAXErrorHandler);
		RELEASE_PTR(pISAXContentHandler);
		RELEASE_PTR(pSAXReader);
		RELEASE_PTR(pMXWriter);

		return SUCCEEDED(hr);
	}