Advertisement

libraries for exchanging xml over high-bandwidth network?

Started by June 06, 2006 03:39 PM
2 comments, last by hplus0603 18 years, 8 months ago
I have a need to exchange streams of data packed in xml format over a local network. I've used libxml to parse xml files in the past, but I don't believe it has any methods for serializing. Does anyone have experience with this sort of thing? Cheers!
What do you need to serialize? An existing DOM, or do you also want a library to marshal arbitrary data structures to XML? Have you looked at various XML-RPC implementations? (it would be worth googling for)
enum Bool { True, False, FileNotFound };
Advertisement
An existing DOM. Essentially I'm tagging metadata to position information with XML so that it can be used by other subscribers.

In other words, instead of sending binary (x,y) I'm sending <data t=12345 x=foo y=bar>sadDsaA323AsSs</data>.
Writing out an existing DOM as text XML is pretty simple, so I'm not surprised if that's left as an exercise for the reader :-)

I'd recommend using a string builder class (or strstream) rather than a std::string, because of the n-squared problem of concatenating to a regular std::string.

Here's a sketch how to do it (in the generic sense):

std::string serialize( XMLNode * root ){  stringbuilder str = "<?xml version='1.0'?>\n";  serialize_internal( root, str );  return str.string();}void serialize_internal( XMLNode * node, stringbuilder & str ){  if( node->type() == TypeData ) {    str += node->value();    return;  }  if( node->type() == TypeCdata ) {    str += "<![CDATA[" + node->value() + "]]>";    return;  }  str += "<" + node->name();  size_t nattr = node->countAttributes();  for( size_t i = 0; i < nattr; ++i ) {    XMLAttribute * a = node->attribute(i);    str += " " + a->name() + "='" + a->value() + "'";  }  size_t ncld = node->countChildren();  if( ncld > 0 ) {    str += ">";    for( size_t i = 0; i < ncld; ++i ) {      serialize_internal( node->child(i), str );    }    str += "</" + node->name() + ">";  }  else {    str += " />";  }}


You may want to entity encode some of the data if your DOM doesn't store the values already encoded (say, to avoid putting a raw quote (') in an attribute value).
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement