Menu

How to get an element value?

SilvioReis
2005-09-23
2013-05-20
  • SilvioReis

    SilvioReis - 2005-09-23

    Hi friends!
    I just discovered TinyXml. I'm trying to use it but I'm stuck with getting the value of an Element.

    I have the Following Xml File:
    <?xml version="1.0" encoding="utf-8"?>
    <options>
        <testfiles>       
            <filecount>3</filecount>
            <files>
                <file name="file1.txt">
                    <filesize>111</filesize>
                </file>
               
                <file name="file2.txt">
                    <filesize>112</filesize>
                </file>
               
                <file name="file3.txt">
                    <filesize>113</filesize>
                </file>
            </files>   
       </testfiles>        
    </options>

    I want to get the file count value. I tried this piece of code:

    TiXmlDocument XmlFile;
                if ( XmlFile.LoadFile( XmlFilePath ) )
                {               
                    TiXmlElement* Root = XmlFile.FirstChildElement( "options" );
                    if ( Root != NULL )
                    {
                        TiXmlElement* TestFiles = Root->FirstChildElement( "testfiles" );
                        if ( TestFiles != NULL )
                        {
                            TiXmlElement* FileCount = TestFiles->FirstChildElement( "filecount" );   
                            if ( FileCount != NULL )
                            {
                                const char* NumberOfFiles = FileCount->Value();   
                            }
                        }
                    }
                }

    the function Value() always return NULL. What Am I missing?

     
    • B Sizer

      B Sizer - 2005-09-24

      The '3' in your example is part of a text node, which is a child of FileCount.

       
    • SilvioReis

      SilvioReis - 2005-09-26

      Hi Kylotan!
      Thanks for your reply. Until I get 'filecount' everything seems to be easy the problem is that I couldn't find a simple example explaning how to get a value of an element. Which function should I use to get the value '3'? Could you please post an example?
      Thanks in advance!

       
      • B Sizer

        B Sizer - 2005-09-26

        Try not to think of it as an element's 'value'. Attributes have values, but elements are containers, for other elements and for text. Would it make sense to ask what the value of 'testfiles' is? If so, is it "3 111 112 113"? Or is it " <filecount>3</filecount><files><file name="file1.txt"><filesize>111</filesize> ..." (etc)

        So you have to be specific and say exactly what you want out of an element. In this case, the element contains exactly one thing: a text node. To be precise, in TinyXML this is a 'TiXmlText' object. If you go to http://www.grinninglizard.com/tinyxmldocs/index.html there is an example at the bottom, showing how text nodes relate to the other elements.

        What you should probably do here, is use 'FirstChild()' on the filecount element to get the text node. Cast that pointer to a "TiXmlText*". Then you can call Value() on that to get the text.

        By the way, why do you need a filecount? It seems a bit unnecessary.

         
    • SilvioReis

      SilvioReis - 2005-09-26

      Finally I managed to get the value.

      TiXmlDocument Doc;
              if ( Doc.LoadFile( OptionsFilePath ) )
              {
                  TiXmlHandle hXml( &Doc );           
                  TiXmlText* Result = hXml.FirstChildElement( "options" ).ChildElement( "testfiles", 0 ).ChildElement( "filecount", 0 ).FirstChild().Text();
                  if ( Result != NULL )
                  {
                      printf( "%s\n", Result->Value() );
                  }
                 
              }

      Thank you for the support.
      PS.: The XmlFile in question is just an example.

       

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.