A RapidXML gotcha

— richardwb on Thursday, March 26, 2009 @ 13:00

I just spent a couple of hours trying to track down why I was seemingly losing data values while modifying nodes in the DOM. It's spelled out in the documentation, but it definitely threw me for a loop (I would consider this behavior surprising). Basically, RapidXML gives you the ability to assign/modify the value of a node and it also likes to create data nodes when you parse a document. When printing, the data node takes precedence over the value.

When you create a document from scratch this likely won't bite you, but when you parse an existing document and neglect to supply the parse_no_data_nodes flag, the parser creates data nodes. If you try to update your values with the value() function, it works just fine (and you can even pull it back out without any problems), but when you use the print() function, the data node (which you haven't updated) will show up instead.

What made this even more difficult to track down was how value() will return the first data node it finds if there isn't a value yet, so it really does seem like you have a value already. I've updated my prior RapidXML entry to include the parse_no_data_nodes flag, which may save someone some time down the road.

comments powered by Disqus