Click here to Skip to main content
15,030,781 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a unsigned short* array and i need to write the values inside that array to an xml file.

I don't know how....:-(
Please help...
Posted
Updated 27-Dec-12 2:58am
v4
Comments
Sergey Alexandrovich Kryukov 27-Dec-12 22:50pm
   
Not a question! And what did you try so far?
—SA

1 solution

Vineeth -

You should be able to use something like:
C++
// WriteXml.cpp : Defines the entry point for the console application.
//

// from http://msdn.microsoft.com/en-us/library/windows/desktop/ms757870(v=vs.85).aspx

#include "stdafx.h"
#include <objbase.h>
#include <tchar.h>
#include <msxml6.h>
#include <sstream> 
#include <string>

using namespace std;

// Macro that calls a COM method returning HRESULT value.
#define CHK_HR(stmt)        do{ hr=(stmt); if (FAILED(hr)) goto CleanUp; } while(0)

// Macro to verify memory allcation.
#define CHK_ALLOC(p)        do { if (!(p)) { hr = E_OUTOFMEMORY; goto CleanUp; } } while(0)

// Macro that releases a COM object if not NULL.
#define SAFE_RELEASE(p)     do { if ((p)) { (p)->Release(); (p) = NULL; } } while(0)

// Helper function to create a DOM instance. 
HRESULT CreateAndInitDOM(IXMLDOMDocument **ppDoc)
{
    HRESULT hr = CoCreateInstance(__uuidof(DOMDocument60), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(ppDoc));
    if (SUCCEEDED(hr))
    {
        // these methods should not fail so don't inspect result
        (*ppDoc)->put_async(VARIANT_FALSE);  
        (*ppDoc)->put_validateOnParse(VARIANT_FALSE);
        (*ppDoc)->put_resolveExternals(VARIANT_FALSE);
    }
    return hr;
}

// Helper function to create a VT_BSTR variant from a null terminated string. 
HRESULT VariantFromString(PCWSTR wszValue, VARIANT &Variant)
{
    HRESULT hr = S_OK;
    BSTR bstrString = SysAllocString(wszValue);
    CHK_ALLOC(bstrString);

    V_VT(&Variant)   = VT_BSTR;
    V_BSTR(&Variant) = bstrString;

CleanUp:
    return hr;
}

//from http://stackoverflow.com/questions/6284524/bstr-to-stdstring-stdwstring-and-vice-versa
BSTR ConvertMBSToBSTR(const std::string& str)
{
    int wslen = ::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
                                      str.data(), str.length(),
                                      NULL, 0);

    BSTR wsdata = ::SysAllocStringLen(NULL, wslen);
    ::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
                          str.data(), str.length(),
                          wsdata, wslen);
    return wsdata;
}

void saveDOM()
{
    HRESULT hr = S_OK;
    IXMLDOMDocument *pXMLDom=NULL;
    IXMLDOMParseError *pXMLErr = NULL;
    BSTR bstrXML = NULL;
    BSTR bstrErr = NULL;
    VARIANT_BOOL varStatus;
    VARIANT varFileName;
	
	string xml = "<r>\n";
    VariantInit(&varFileName);

    CHK_HR(CreateAndInitDOM(&pXMLDom));

	// create an array of 10 bytes
	BYTE shorts[10];
	for(int i = 0; i < 10; i++) {
		shorts[i] = (BYTE)i;
	}
	
	

	// what we are using to convert our bytes to string
	// from http://www.cplusplus.com/articles/D9j2Nwbp/
	// we just have to translate our array of unsigned shorts to text

	for(int i = 0; i < 10; i++) {
		ostringstream convert;
		xml += "<a>\n";
		convert << (int)shorts[i];
		xml += convert.str();
		xml += "</a>\n";
		
	}


	// close our xml strring
	xml += "</r>";

    bstrXML = ConvertMBSToBSTR(xml);
    CHK_ALLOC(bstrXML);
    CHK_HR(pXMLDom->loadXML(bstrXML, &varStatus));

    if (varStatus == VARIANT_TRUE)
    {
        CHK_HR(pXMLDom->get_xml(&bstrXML));
        printf("XML DOM loaded from app:\n%S\n", bstrXML);

        VariantFromString(L"myData.xml", varFileName);
        CHK_HR(pXMLDom->save(varFileName));
        printf("XML DOM saved to myData.xml\n");
    }
    else
    {
        // Failed to load xml, get last parsing error
        CHK_HR(pXMLDom->get_parseError(&pXMLErr));
        CHK_HR(pXMLErr->get_reason(&bstrErr));
        printf("Failed to load DOM from xml string. %S\n", bstrErr);
    }

CleanUp:
    SAFE_RELEASE(pXMLDom);
    SAFE_RELEASE(pXMLErr);
    SysFreeString(bstrXML);
    SysFreeString(bstrErr);
    VariantClear(&varFileName);
}




int _tmain(int argc, _TCHAR* argv[])
{
	HRESULT hr = CoInitialize(NULL);
    if(SUCCEEDED(hr))
    {
        saveDOM();
        CoUninitialize();
    }


	return 0;
}

</string></sstream></msxml6.h></tchar.h></objbase.h>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900