Jump to content
  • Advertisement
Sign in to follow this  
Silex

libraries for exchanging xml over high-bandwidth network?

This topic is 4453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!