SOL9 Sample: SAXXMLStringWriter

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SAXXMLStringWriter.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2011/02/03

// Sample program to write an xml-data to a string stream by using SAXXMLWriter class.

// 2011/09/08 Updated to specify "utf-8" for xml-encoding in Windows7 x64 OS.

#include <sol/COMInitializer.h>
#include <sol/Locale.h>
#include <sol/xml/SAXXMLWriter.h>


void _tmain(int /*argc*/, TCHAR** /*argv*/)
{
  COMInitializer comInitializer;
  Locale locale;
  try {

    SAXXMLWriter writer;
    //2011/09/08 
    //writer.putEncoding("utf-16");
    writer.putEncoding("utf-8");

    writer.putStandalone(VARIANT_TRUE);

    //Create a string stream on a memory
    writer.create();

    writer.startDocument();

    writer.startElement("root");

    SAXXMLAttributes attrs1;
    attrs1.addAttribute("name", "Foo");
    attrs1.addAttribute("email", "foo@foo.com");
    attrs1.addAttribute("department", "ipad");

    writer.startElement("developer", attrs1);
    writer.characters("This is a foo developer.");
    writer.endElement("developer");

    SAXXMLAttributes attrs2;
    attrs2.addAttribute("name", "Someone");
    attrs2.addAttribute("email", "someone@foo.com");
    attrs2.addAttribute("department", "android");

    writer.startElement("developer", attrs2);
    writer.characters("This is a someone developer.");
    writer.endElement("developer");

    writer.endElement("root");

    writer.endDocument();

    //Get XML string from a string stream in the writer.
    _bstr_t xml;
    writer.getXML(xml);

    _tprintf(_T("XML=%s\n"), (const TCHAR*)xml);

  } catch (HRESULT hr) {
    _tprintf(_T("Exception 0x%x\n"), hr);
  } catch (Exception& ex) {
    ex.printf();
  } catch (_com_error& ex) {
    ComError error(ex);
    error.printf();
  } catch (...) {
    _tprintf(_T("Exception %x\n"), GetLastError());
  }

}

Last modified: 2 May 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.