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 3:56am
Edited 27-Dec-12 3: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

<h1>include "stdafx.h"</h1>
 
<h1>include <objbase.h></h1>
 
<h1>include <tchar.h></h1>
 
<h1>include <msxml6.h></h1>
 
<h1>include <sstream></h1>
 
<h1>include <string></h1>
 
using namespace std;
 
// Macro that calls a COM method returning HRESULT value.
<h1>define CHK_HR(stmt)        do{ hr=(stmt); if (FAILED(hr)) goto CleanUp; } while(0)</h1>
 
// Macro to verify memory allcation.
<h1>define CHK_ALLOC(p)        do { if (!(p)) { hr = E_OUTOFMEMORY; goto CleanUp; } } while(0)</h1>
 
// Macro that releases a COM object if not NULL.
<h1>define SAFE_RELEASE(p)     do { if ((p)) { (p)->Release(); (p) = NULL; } } while(0)</h1>
 
// 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);
 
<pre>
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 &lt; 10; i++) {
    shorts[i] = (BYTE)i;
}
 
// what we are using to convert our bytes to string
// from <a href="http://www.cplusplus.com/articles/D9j2Nwbp/">http://www.cplusplus.com/articles/D9j2Nwbp/</a>
// we just have to translate our array of unsigned shorts to text

for(int i = 0; i &lt; 10; i++) {
    ostringstream convert;
    xml += "<a>\n";
    convert &lt;&lt; (int)shorts[i];
    xml += convert.str();
    xml += "</a>\n";
 
}
 
// close our xml strring
xml += "</r>";
 
bstrXML = ConvertMBSToBSTR(xml);
CHK_ALLOC(bstrXML);
CHK_HR(pXMLDom-&gt;loadXML(bstrXML, &varStatus));
 
if (varStatus == VARIANT_TRUE)
{
    CHK_HR(pXMLDom-&gt;get_xml(&bstrXML));
    printf("XML DOM loaded from app:\n%S\n", bstrXML);
 
    VariantFromString(L"myData.xml", varFileName);
    CHK_HR(pXMLDom-&gt;save(varFileName));
    printf("XML DOM saved to myData.xml\n");
}
else
{
    // Failed to load xml, get last parsing error
    CHK_HR(pXMLDom-&gt;get_parseError(&pXMLErr));
    CHK_HR(pXMLErr-&gt;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;

}

  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 490
1 Sergey Alexandrovich Kryukov 325
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200
0 OriginalGriff 465
1 Sergey Alexandrovich Kryukov 275
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200


Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2015
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