Sign in to follow this  
Silex

libraries for exchanging xml over high-bandwidth network?

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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>.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this