Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFC
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....Frown | :-(
Please help...
Posted 27-Dec-12 2:56am
Edited 27-Dec-12 2:58am
v4
Comments
Sergey Alexandrovich Kryukov at 27-Dec-12 22:50pm
   
Not a question! And what did you try so far?
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Vineeth -
 
You should be able to use something like:
// 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>
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 166
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,077
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100