Menu

regarding XML parsing!!

Developer
honeybie
2007-01-03
2013-05-20
  • honeybie

    honeybie - 2007-01-03

    Hi all,

    I am newbie for XML and working on c++ platform
    This is my XML file (Hope, it is correct!)

    <?xml version="1.0" encoding="utf-8" ?>
    <users source="URL" challenge="EncodedString" session="String">
        <user name="ABC" password="abc"/>
        <user name="XYZ" password="xyz" />  
        <source level="">URL</source>
        <default level="" />
    </users>

    I want to parse this file to get name and password(for now) I have tried upto this:

    void XmlParser::Loader(const char* pFilename)
    {
        file.open ("c:/hello.txt", ios::out | ios::in | ios::trunc);   

        TiXmlDocument tiDocument(pFilename);
        bool success = tiDocument.LoadFile();
        if ( !success )
            {
                printf( "Failed to load file \"%s\"\n", pFilename );
                exit( 1 );
            }
        TiXmlHandle hDoc(&tiDocument);
        TiXmlHandle hand = hDoc.FirstChildElement("users");
        Parser(tiDocument);
        ooFile.close();
    }
    void XmlParser::Parser(TiXmlHandle handle)
    {
        
        TiXmlElement* ele;
        ele = doc.FirstChildElement( "users" ).Element();
        const char* username=ele->GetText();    <- Problem is here!!
        file << username;
        ele=ele->NextSiblingElement();
        const char *password=ele->GetText();
    }

    Problem is: in const char* username, no data is stored!! Please help me to solve this!

    Regards

     
    • honeybie

      honeybie - 2007-01-03

      Please refer this for parser function!

      void XmlParser::Parser(TiXmlHandle handle)
      {

      TiXmlElement* ele; 
      ele = handle.FirstChildElement( "users" ).Element();
      const char* username=ele->GetText(); <- Problem is here!!
      file << username;
      ele=ele->NextSiblingElement();
      const char *password=ele->GetText();
      }

       
    • Ellers

      Ellers - 2007-01-04

      You are obtaining the first child element called "users" and asking for the text of it.
      Your <users> node has no text (well, it has whitespace), just elements.
      First, obtain the handle to a "user" element, then get the password attribute.

      Or, get the users element, then get its first child element (a user element), then the next child element, etc.

      HTH

       
    • honeybie

      honeybie - 2007-01-04

      I tried by changing user instead of users. But it is showing the same error!! Actually I am not clear with parsing.(even if I have read many documents :( )

      Can you please write the snippet according to the given code so that I can have an idea for the other xml files! Plzzzzzzzzzzzzzzzzzzzz!

      Thanks a lot in advance
      regards

       
    • honeybie

      honeybie - 2007-01-04

      I changed my code by this

              TiXmlDocument tiDocument(pFilename);
          bool success = tiDocument.LoadFile();
          TiXmlHandle hDoc(&tiDocument);
          TiXmlHandle hRoot(0);
          TiXmlElement* pElem;
          pElem=hDoc.FirstChildElement().Element();
          hand=TiXmlHandle(pElem);
          int i;
          TiXmlElement* ele = hRoot.FirstChild( "user" ).FirstChild().Element();
          for( ele; ele; ele=ele->NextSiblingElement())
          {
          const char* name = ele->Attribute("name");
          configFile << name <<"\n";   
          const char* password=ele->Attribute("password");
          configFile << password <<"\n";   
          }   
      Now that exception error has gone but I am not getting name and password in my file

      What's wrong here? kindly help

      regards   

       

Log in to post a comment.