Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Simple C++ class for XML writing

, 4 Dec 2003
Tiny template-based C++ class, which simplifies writing of XML data.
xmlwriter.zip
//////////////////////////////////////////////////////////////////////
//
// Example of using XmlStream class.
// 
// Author: Oboltus, December 2003
//
// This code is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
//////////////////////////////////////////////////////////////////////

#include <fstream>
#include "xmlwriter.h"

using namespace std;
using namespace	xmlw;

int main()
{
  ofstream f("sample.xml");
  XmlStream xml(f);

  xml << prolog() // write XML file declaration
    << tag("sample-tag") // root tag

      << tag("some-tag") // child tag
        << attr("int-attribute") << 123
        << attr("double-attribute") << 456.789
        << chardata() << "This is the text"
      << endtag() // close current tag

      << tag("empty-self-closed-tag") << endtag() // sibling of <some-tag>

      << tag() << "computed-name-tag" << attr("text-attr") << "a bit of text"
      << endtag()

      << tag("deep-tag") // deep enclosing
        << tag("sub-tag-2")
          << tag("sub-tag-3")
      << endtag("deep-tag"); // close all tags up to specified

  // look: I didn't close "sample-tag", it will be closed in XmlStream destructor

  return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Oboltus
Web Developer
Russian Federation Russian Federation
Programming since 1992
Favorite programming language: C++
Main interest (currently): computer modeling of physical processes in radiation-damaged metalls

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 5 Dec 2003
Article Copyright 2003 by Oboltus
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid