TinyXML print to string

Started by
6 comments, last by evolutional 18 years, 1 month ago
hello, TinyXml has a Print() function that saves the xml to a file. but i would need the xml in my program stored in a std::string or a char* how can i do that? thanks!
Advertisement
You could probably do it yourself fairly easily by looking at the code in the TiXmlElement::Print method and providing your own implementation to append to a std::string.
yeah, i was thinking about that, but is not that easy, since this Print() function depends on a lot of other stuff which all uses the same filepointer.

of course it would be doable, but i was looking for an easier way to just redirect the stuff that is written to the filepointer to a string.

thanks!
Why do you need the raw XML contained in a string?
because i am sending it to a server over the network. and it is discarded right after that, so i there is no need to save it to a file.
So let me get this straight first:

- You have generated your own XML inside your application (as opposed to loading it in from a file) or have modified a loaded XML file?

- You want to send the entire thing over the network, not just a particular node?

If so, i can think of two ways to get it done:

1) Print the whole tree to a file, then send the file over the network. (pro: simple. con: slow)

2) loop through each element in the tree and print each element to a string/stream. The << and >> operators are usable on all elements (consult your documentation) (pro: faster speed. con: more code needed)
ehmdjii,

You could write XML directly to the socket. It appears that the Print function takes in a FILE*. Sockets are ints (or, ya know, SOCKETs, if you are using Windows). In other words, they are file descriptors. If you call fdopen on a data socket, it will give you a FILE* that you can use with the Print function. It won't know the difference. ;)

Vovan
Vovan
All TiXmlNode elements have a << operator that takes an ostream meaning it seems that it will write the XML to stream of your choice (eg: stringstream). See documentation for notes.

This topic is closed to new replies.

Advertisement