Menu

How to get the content of TiXmlDocument?

Anonymous
2003-01-31
2004-01-04
  • Anonymous

    Anonymous - 2003-01-31

    Subj?
    Do I have to stick with streams? It seems to be odd...
    All I want is to construct simple XML doc and then send it to other computer, why isn't there GetAsStr() or something like that? And this (or innerXML) could be useful in TiXMLElement also...
    Waiting for your comments...

     
    • B Sizer

      B Sizer - 2003-02-02

      Probably the best way to do it is to use a stringstream. Then you can use the .str() method of the stringstream to send the text. This will handle the memory allocation for the string for you, whereas a GetAsStr() function would end up reimplementing a lot of this functionality.

      Alternatively, you could make sure TIXML_USE_STL is not defined. This should mean that the TIXML_OSTREAM is actually a TiXmlOutStream, which is a thin stream-like wrapper around a TiXmlString, which in turn has a c_str () function for returning the data that you require.

      Hope this helps - I'm new to this library too so I'm largely just going on what I can see from browsing the docs. If you have any success, post a reply so we know how you did it. :)

       
      • Anonymous

        Anonymous - 2003-03-25

        Sorry, I think I currently have the same problem, but I don't understand your answer. What is a stringstream ?
        Isn't there any way to get the data into a string or char* type variable?
        I have generated a xml-file and now want to send it to other computers without the indirection of saving and loading it before.
        Thanks for your efforts!

        Cheers,
        Jochen

         
        • B Sizer

          B Sizer - 2003-04-01

          stringstreams are in <sstream>.

          Imagine cin or cout, except that instead of writing or reading to the screen, they write or read from a std::string (a wrapper for char*).

          Code done from memory, so don't shoot me if there are bugs. And obviously put it in a function :)

          #include <sstream>
          ostringstream myStream;
          // Extract data from xmlDocument
          myStream << xmlDoc;
          const char* xmlAsText = myStream.str().c_str();

           
    • Anonymous

      Anonymous - 2003-02-03

      Thanx, but I've seen this way and thought that it would be more useful to have some more straightforward mechanism

       
      • B Sizer

        B Sizer - 2003-02-03

        Remember that this is 'Tiny' XML. Adding in the code to manage a dynamic string buffer when the standard library already offers this functionality in stringstreams is probably not what the library is all about. Either way, whether you do it with STL or not, it's still only 3 or 4 lines of code, which you can wrap in your own function in less than 10 minutes.

         
    • Pedro Canales

      Pedro Canales - 2004-01-02

      Easy man!

      Do it as kylotan says.
      Actually there is an example in the test code provided by tinyxml.

      In the file xmltest.cpp look for the string "streaming", there you will find a snippet of working code.

      I used this, and works :)
      Next code converts a document (TiXmlDocument) to a const char *

      #ifdef TIXML_USE_STL
          cout << "** Basic structure. **\n";
          ostringstream outputStream( ostringstream::out );
          outputStream << doc;
          //XmlTest( "Output stream correct.",    string( demoEnd ).c_str(), outputStream.str().c_str(), true );
          printf (" %s\n", outputStream.str().c_str());
      #endif

      //where "doc" is an already loaded TiXmlDocument object.

      Just make sure that you have added the next line in the file "tinyxml.h" :  
      #define TIXML_USE_STL

      This line, tells tinyxml that use support for stl, without this you will be unable to use stream support.
      This work on Windows.
      On linux you would have to set an environment variable like this: TINYXML_USE_STL=YES/NO

      Good look.

       
    • Simon Harrison

      Simon Harrison - 2004-01-02

      Don't be an idiot and spend an hour figuring out why stringstream performance suck for large XML files on Windows.  If you have this problem edit VC98\INCLUDE\sstream and change:

      size_t _Ns = _Os + _Alsize;

      to:

      size_t _Ns = (_Os &lt; _Alsize) ? _Os + _Alsize : 2 * _Os + 1;

      The buffer needs to grow exponentially in size, rather than linearly.  In practice this is only important for documents larger than about 100Kbytes.

      I think recent versions of gcc std++ don't have this problem.

       
    • Simon Harrison

      Simon Harrison - 2004-01-02

      I meant:

      size_t _Ns = (_Os < _Alsize) ? _Os + _Alsize : 2 * _Os + 1;

       
    • Lee Thomason

      Lee Thomason - 2004-01-04

      In the version 2.2.1, TinyXml supports operator<< to a string. That's essentially a "Get a unformatted string" function, if you are using the STL version.

      lee

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.