Click here to Skip to main content
Click here to Skip to main content

Simple C++ class for XML writing

, 4 Dec 2003
Rate this:
Please Sign up or sign in to vote.
Tiny template-based C++ class, which simplifies writing of XML data.

Introduction

In some of my programs, I have to write the data in XML format. Only writing: reading and parsing is the mission of some other tools. Actually I only have to write the simple plain text with some specific markup.

For these purposes, I have implemented a simple class, which behaves as usual std::stream, but also track all the needed XML markings. The class implementation is very simple and template-based. One may write to XmlStream, everything that can be written into std::ostream. The tags and attributes are declared by special helper functions.

Using the code

Look at the example below, which tells everything about my class (I specially omitted the implementation of the XmlStream class, if you’ve interest in it, you'll download the source code, I think).

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") // sibling of <some-tag>
    << endtag()

    << 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

// you don't worry about closing all open tags!

As you can see, writing of XML became a very simple procedure. XmlStream delegates all the incoming data to a specified std::ostream (std::ofstream in the example above) except for the tags and attributes declarations, which are issued by helper functions: tag(), endtag(), attr(), chardata() and prolog(). To fill attribute values and tag character data, one should use standard stream operations. If object of some class can be written into std::ostream, it's automatically writeable into my XmlStream. This feature is released by template operator<<, which mostly calls standard streaming operations.

template<class t> XmlStream& operator<<(const t& value) {
  // for computed-name tags we have to remember written data
  if (stateTagName == state)
    // tagName is object of std::ostringstream class
    tagName << value;
  // now call specified std::stream which
  // performs actual output  
  s << value;
  return *this;
}

The final executable contains only those implementations of this operator, which corresponds to actually used data types.

Advantages of my approach

  • Writing of XML data is very fast (almost as fast as plain text writing).
  • You use standard and well-known functions to build the content of the XML document. It's simple to implement new operator's supporting any user classes.
  • The source code, which writes XML data, represents itself the structure of the resulting document.
  • XmlStream is a really tiny class! Almost all of its work is placed in one overloaded operator<<.

Disadvantages

  • All the data are written into XML "as is", without any encoding (UTF-8, UTF-16 etc.). In my tasks, it's often not a matter of headache, because my data are simply either Latin alphas or numbers. But for the common case, one may have to implement special std::ostream (in fact: std::streambuf) successor, which will perform the encoding work.
  • XmlStream does not check if output XML data represent well-formed XML document. Programmer must check himself whether the tag and attribute names are valid and there are no special characters (like angle brackets) in the attribute values and character data.

Futures

The current version of XmlWriter is used in my programs, which outputs the results of the scientific calculations in XML format. If someone wants to improve this simple class, he may first get rid of disadvantages mentioned above. Also he might change the processing of the tags to make output XML documents more human-readable (i.e. add line separators, padding etc).

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

Comments and Discussions

 
GeneralMy vote of 5 PinmemberOrlando Selenu2-Jul-13 0:31 
Questiontwo small modifications PinmemberOrlando Selenu2-Jul-13 0:31 
Questioncant find .exe in xml writing Pinmemberyidiiss8-Nov-11 6:35 
GeneralGood, waiting for the opposite direction PinmemberCPallini8-Nov-07 21:16 
GeneralRe: Good, waiting for the opposite direction Pinmemberyidiiss8-Nov-11 6:42 
GeneralRe: Good, waiting for the opposite direction Pinmemberyidiiss8-Nov-11 6:42 
GeneralLove the code - here's a tiny readability mod [modified] Pinmemberalan.witmer10-Oct-07 3:31 
QuestionAppend tag into xml: Pinmemberchuansiong4-Oct-07 1:14 
QuestionIn unix PinmemberSinthiya Marian9-Feb-07 23:16 
Generalreader xml in c++ PinmemberDang Viet Dat23-Dec-06 2:10 
GeneralWoW!!!!!!!! Pinmembershahsani8345-Nov-06 14:04 
GeneralloadLibrary error PinmemberRadia624-May-06 16:50 
Generalxmlwriter details PinmemberBernd Speiser11-Sep-05 7:16 
QuestionUnicode? PinmemberAbu Mami11-Mar-05 1:40 
GeneralUse conditions Pinmemberdmaximov22-Apr-04 21:48 
GeneralRe: Use conditions PinmemberOboltus26-Apr-04 23:05 
GeneralRe: Use conditions Pinmemberdmaximov27-Apr-04 18:29 
Generalread XML Pinmemberblablakam28-Jan-04 4:41 
GeneralRead XML! PinmemberM.Khadem18-Dec-03 0:59 
your code can be very usefuller than now if it can read xmls in this simple way.isn't it? Smile | :)
GeneralXML Reader PinmemberNagesh P10-Dec-03 23:49 
GeneralRe: XML Reader PinmemberOboltus11-Dec-03 0:02 
GeneralMicrosoft XMLDOM interface PinmemberNadiaTariq10-Dec-03 1:02 
GeneralRe: Microsoft XMLDOM interface PinmemberOboltus10-Dec-03 9:01 
GeneralRe: Microsoft XMLDOM interface PinmemberVince C.10-Dec-03 10:13 
GeneralSimplest is usually the best... PinmemberVince C.9-Dec-03 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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