Menu

how do I get doc into a buffer?

Rick Zemer
2005-10-12
2013-05-20
  • Rick Zemer

    Rick Zemer - 2005-10-12

    This is probably trivial, however... it has be befuddled:

    I have a TinyXML document that I need to copy/move to a memory buffer. (Don't ask:)  I don't seem to be able to get the .Print to work with a streamBuffer, etc.  Any solutions  / suggestions, methods,  etc greatly appreciated.

    Thanks!
     

     
    • Yves Berquin

      Yves Berquin - 2005-10-13

      Here's the function I'm using :

      std::string get_xml_string (const TiXmlNode * node)
      {
         std::string s;
         std::ostringstream os_stream (std::ostringstream::out);

         os_stream << * node;
         s = os_stream . str ();
         return s;
      }

       
      • Rick Zemer

        Rick Zemer - 2005-10-13

        Thanks!

        ... but... more idiocy (on my part:)...

        I get the following error message:
        c:\Dvr\eClient\Config.cpp(334): error C2027: use of undefined type 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'
                with
                [
                    _Elem=char,
                    _Traits=std::char_traits<char>,
                    _Alloc=std::allocator<char>
                ]
                Config.cpp(334) : see reference to class template instantiation 'std::basic_ostringstream<_Elem,_Traits,_Alloc>' being compiled
                with
                [
                    _Elem=char,
                    _Traits=std::char_traits<char>,
                    _Alloc=std::allocator<char>
                ]

        I'm using Visual Studio V7 on XP.

        Thanks very much for your help!

        -rz.

         
        • Yves Berquin

          Yves Berquin - 2005-10-13

          You may just miss the following headers :

          #include <iostream>
          #include <sstream>

           
    • Lee Thomason

      Lee Thomason - 2005-10-13

      If you are adventureous, there are patches (in the Patches section) to do this as well. It's a common feature request to have an API for this - one we'll consider for the next version.

      lee

       
    • Rick Zemer

      Rick Zemer - 2005-10-13

      I'd like to be adventureous, but -- um, how?
      I don't see any file/patch although there is a patch entry is listed in the patches section. 

      1). what do I download (from where)?
      2). what do I do with it after I download it?

      Stupidity continues...

      Thanks!

       
    • Lee Thomason

      Lee Thomason - 2005-10-13

      Well, normally you would click "Patches" above, then "Save xml to memory buffer path for release 2.3.4". And then it would depend on what kind of patch it is.

      But since the patch doesn't seem to have a file attached (submitter forgot it?) it isn't going to work regardless. :) And hopefully you are on 2.4 in any case.

      The STL solution (from Yves, above) is the way to go on STL. If you are not using STL, I would suggest saving to a temp file and then reading the temp file to a memory buffer.

      lee

       
      • Rick Zemer

        Rick Zemer - 2005-10-14

        hmmm...
        Apparently, I'm missing something (perhaps a large portion of my cerbral cortex or other large bundle of neurons:), but my compiler complains : binary '<<': no operator found which takes a righ-hand operand of type 'TiXmlNode' (or there is no acceptable conversion).

        My (relevant) code below, which I believe to be almost verbatim the earlier solution (with context).  When I remove the dereference (i.e., << rootnode ), it compiles, but the output is a single hex num (the address of the node).

        What am I missing, or do I just need a firm whack on the side of the head?

        Thanks everyone for your help and especially patience...
        -rz.

        int initConfiguration( void )
        {
            TiXmlNode * rootnode = 0;
            TiXmlDocument doc;
            TiXmlElement * usersElement = 0;
            TiXmlElement * userItem = 0;
            char * p;
            char * name;
            char * uid;
            char * pwd;
            char * plvl;
            int    num_users = 0;
             std::string s;
            std::ostringstream os_stream (std::ostringstream::out);

            //Parse the XML file and load member vectors
            if ( !doc.LoadFile( "users.xml" ) )
            {
                printf( "EDVRCLIENT: mainFrame::ReadConfig config file not found\n" );
                return 0;
            }

            rootnode = doc.FirstChild( "config" );
            if ( rootnode )
            {
                usersElement = static_cast<TiXmlElement*> ( rootnode->FirstChild( "users" ) );
                if ( usersElement )
                {
                    userItem = static_cast<TiXmlElement *>( usersElement->FirstChild( "user" ) );
                    while ( userItem )
                    {
                        num_users++;
                        name = (char *)userItem->Attribute( "name" );
                        uid = (char *)userItem->Attribute( "userid" );
                        pwd = (char *) userItem->Attribute( "password" );
                        userItem = static_cast<TiXmlElement*>( userItem->NextSibling( "user" ) );
                    }
                }
            }

            os_stream << (* rootnode);

            s = os_stream.str();

            cout << s << endl;

            return true;
        }

         
    • Lee Thomason

      Lee Thomason - 2005-10-18

      I think you need to turn on STL support. :)

      #define TIXML_USE_STL

      And be sure to read the docs on "Using STL".

      lee

       
      • Rick Zemer

        Rick Zemer - 2005-10-18

        AHA!

        Well, sort of... That was part of the problem, having the
        #include <tinystr.h> before the <tinyxml.h> wasn't helping either...

        Thanks everyone for your patience, AND your help.  It is greatly appreciated (as is this software).

        Perhaps when one or more of the plethora of fires burning is extinguished, I'll be able to reflect on all this and recoil in horror at how <expletive> stupid I have been. 

        'Til then...

        Thanks again.

         

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.