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

  1. Layne
    Friday, March 26, 2010 @ 12:21

    Oh my gosh... I was looking into this problem for a long time and couldn't figure out why I wasn't able to overwrite values.

    You really saved me! Many other parser are just not fast enough.

  2. wxml
    Monday, August 9, 2010 @ 16:37

    THANKS. I tried to figure this out for at least two hours by now. Failed to see that documentation-passage.

  3. What Haveyou
    Tuesday, January 4, 2011 @ 14:39

    You are too kind, calling it "surprising."

    I'd call it "stupid."

  4. b
    Monday, July 4, 2011 @ 06:44

    b

  5. QuantumState
    Friday, October 7, 2011 @ 09:43

    Nice! Thanks for this. I spent an hour stepping through the rapidxml_print.hpp, trying to figure this out. The weird thing is if you keep the data nodes and try to print, it doesn't ever actually hit the "print_data_node" function in rapidxml_print.hpp. Instead it just silently prints the data node from inside the "print_element_node" function. Crazy!

Add a comment
(?)

BBCode