Like Tree1Likes
  • 1 Post By Salem

TinyXML reading XML and storing to string array

This is a discussion on TinyXML reading XML and storing to string array within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm trying to use TinyXML to read a XML and store some of it's values to a string array. ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    38

    Question TinyXML reading XML and storing to string array

    Hello, I'm trying to use TinyXML to read a XML and store some of it's values to a string array.

    How would I do this?

    So far this is my code: (note you can pretty much just skip to my int main() since everything else is good)

    Code:
    #define TIXML_USE_STL
    #include "tinyxml.h"
    #ifdef TIXML_USE_STL
     #include <iostream>
     #include <sstream>
     using namespace std;
    #else
     #include <stdio.h>
    #endif
    #if defined( WIN32 ) && defined( TUNE )
     #include <windows.h>
     __int64 start;
     __int64 end;
     __int64 freq;
    #endif
    static int gPass = 0;
    static int gFail = 0;
    
    bool XmlTest (const char* testString, const char* expected, const char* found, bool noEcho = false)
    {
     bool pass = !strcmp( expected, found );
     if ( pass )
      printf ("[pass]");
     else
      printf ("[fail]");
     if ( noEcho )
      printf (" %s\n", testString);
     else
      printf (" %s [%s][%s]\n", testString, expected, found);
     if ( pass )
      ++gPass;
     else
      ++gFail;
     return pass;
    }
    
    bool XmlTest( const char* testString, int expected, int found, bool noEcho = false )
    {
     bool pass = ( expected == found );
     if ( pass )
      printf ("[pass]");
     else
      printf ("[fail]");
     if ( noEcho )
      printf (" %s\n", testString);
     else
      printf (" %s [%d][%d]\n", testString, expected, found);
     if ( pass )
      ++gPass;
     else
      ++gFail;
     return pass;
    }
    
    int main()
    {
        TiXmlDocument doc( "demotest.xml" );
        TiXmlElement * element = new TiXmlElement( "Bold" ); 
        doc.LinkEndChild( element );
        cout << element; //i'm just doing cout for now so i can see that a word is stored and not some random bs.
        system("PAUSE");
    }
    The out put it get is:
    0x4a1098Press any key to continue...
    my xml file looks like this
    <?xml version="1.0" standalone="no" ?>
    - <!--
    Our to do list data -->
    - <ToDo>
    - <!--
    Do I need a secure PDA? -->
    - <Item priority="1" distance="close">
    Go to the
    <bold>Toy store!</bold>

    </Item>


    <Item priority="2" distance="none">Do bills</Item>

    <Item priority="2" distance="far & back">Look for Evil Dinosaurs!</Item>

    </ToDo>


  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,490
    std:: ostream has no idea what to do with a TiXmlElement pointer. it just looks like any other pointer from ostream's perspective. try dereferencing it.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    im not expereinced with dereferencing, is it something like this?

    Code:
    int main()
    {
        TiXmlDocument doc( "demotest.xml" );
        TiXmlElement * element = new TiXmlElement( "Bold" ); 
        doc.LinkEndChild( element );  
        string*= exforsys;
        string = test;
        exforsys=&element;
        test=*exforsys;
        cout << *exforsys << endl << test; 
        system("PAUSE");
    }

    2nd attempt:
    Code:
    int main()
    {
    TiXmlDocument doc( "demotest.xml" );
    TiXmlElement * element = new TiXmlElement( "Bold" ); 
    doc.LinkEndChild( element ); 
    
    std::string test = element; 
    string x = &element; 
    string y=*x; 
    cout << *element << endl << test; 
    
    system("PAUSE");
    }
    Last edited by airesore; 10-26-2011 at 04:27 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,490
    the second attempt is better, but your assignment of a TiXmlElement** to a string is wrong. they are different types and you cannot convert between them. element is a pointer, and when you say &element, you're getting the address of that pointer, so it becomes a TiXmlElement** type. get rid of all that stuff with strings, and see if it works. I don't know TinyXML at all, but I know that you can't do that.

    Code:
    cout << *element << endl;
    seems like it has the best chance of working, but I can't say, because I'm not familiar with TinyXML.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Does anybody read the manual before blundering around in the dark typing in random stuff and calling it a program?

    > TiXmlElement * element = new TiXmlElement( "Bold" );
    Great, you found the constructor.

    Now read this -> TinyXml: TiXmlElement Class Reference
    and see if you can scroll down far enough to read
    const char * GetText () const
    Convenience function for easy access to the text inside an element.
    rags_to_riches likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    I'm still not getting this. I know it's simple, but I'm quite novice.

    Code:
    int main()
    {
        TiXmlDocument doc( "demotest.xml" );
        TiXmlElement * element = new TiXmlElement( "Bold" ); 
        doc.LinkEndChild( element );  
        const char* str = element->GetText();
        cout << str << endl; 
        system("PAUSE");
    }
    I get this error 'class TiXmlElement' has no member named 'GetText'

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    That appears to contradict the documentation that Salem linked to. What version of TinyXML are you using?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    I'm not sure, but i got it from this site. Using TinyXML with Dev-C++, C++ Libraries and Application Frameworks
    The .cpp came linked already with tinyxml.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Better get the latest then - TinyXML | Free Development software downloads at SourceForge.net
    2.3.4 vs 2.6.2

    Probably a good few years out of date if it's pre-build for dev-c++.

    Or you could see if there is a manual for the version you're using, or even just read the class header/implementation for something which "looks" like it might work the same.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Long story story, i am using code blocks now...>.>

    Where's the lib file in the tinyxml download zip for 2.6.2? Name:  Untitled.png
Views: 1915
Size:  11.7 KB
    I'm trying to link it however i can't find the lib file.

    EDIT:Nvm I found my answer...

    EDIT2: Still the same error...
    error: 'class TiXmlElement' has no member named 'GetText'
    and i'm using code blocks 7.4 and TinyXml 2.6.2.
    Last edited by airesore; 10-27-2011 at 12:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-01-2011, 07:57 PM
  2. Replies: 12
    Last Post: 09-23-2010, 11:49 AM
  3. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  4. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21