Menu

TiXmlHandle in a loop

Phil
2005-08-02
2013-05-20
  • Phil

    Phil - 2005-08-02

    hi,

    sorry for my many questions, i dont know how to solve my next problem. i have an easy xml file like:

    <?xml version="1.0" encoding="UTF-8"?>
    <product>
    <a001>A30</a001>
    <productidentifier><b221>01</b221><b244>2000407</b244></productidentifier><productidentifier><b221>02</b221><b244>3406428649</b244></productidentifier><productidentifier><b221>03</b221><b244>9783406428647</b244></productidentifier>
    <b246>01</b246>
    </product>

    Now, i want to read all data in the b244 tags.

    this is my way:

    bool __fastcall ParseXML()
    {
       TiXmlDocument doc("c:\\test.xml");
       doc.LoadFile();
       TiXmlHandle handle(&doc);
       TiXmlText *result;

       for (int i = 0; ; i++)
       {
          result = handle.ChildElement("product", 0).ChildElement("productidentifier", i).ChildElement("b244", 0).FirstChild().Text();
          if (result)
             ShowMessage(result->Value());
          else
             break;
       }

       return true;
    }

    this do not work. i get no data. my first test example with a loop works fine, there was no third childelement after my dynamic second childelement.

    could anybody tel me why this example would not work?

    Phil

     
    • Ellers

      Ellers - 2005-08-02

      Well, this for loop is not helping you:

      for (int i = 0; ; i++)

      See anything missing here!? 8)

      Ellers

       
    • Phil

      Phil - 2005-08-02

      Hi ellers,

      this loop works fine, here another example:

      for (int i = 0; ; i++)
      {
         ShowMessage(i);

         if (i > 5)
            break;
      }

      I need an endless loop with counter, this does my for loop. so, this cant be the error :)

      phil

       
    • Ellers

      Ellers - 2005-08-02

      Ah, fair enough, my bad.

      Well, I used your exact xml, made a new console exe and modified your code only slightly to be this:

      void test3()
      {
          TiXmlDocument doc( "demo_20050802.xml" );

          doc.LoadFile();
          TiXmlHandle handle(&doc);
          TiXmlText *result;

          for (int i = 0; /* NONE */ ; i++)
          {
              result = handle.ChildElement("product", 0).ChildElement("productidentifier", i).ChildElement("b244", 0).FirstChild().Text();
              if (result)
              {
                  printf( "Value for i=%d: [%s]\n", i, result->Value());
              }
              else
              {
                  // finish the loop
                  break;
              }
          }
          printf( "Finished\n" );
      }

      and got these results, which look correct from your data:

      Value for i=0: [2000407]
      Value for i=1: [3406428649]
      Value for i=2: [9783406428647]
      Finished

      Perhaps your ShowMessage() is not working? Have you tried stepping through in a debugger?

      Ellers

       
    • Phil

      Phil - 2005-08-04

      hi,

      now the same code works fine as well in my application. i think i should not always working until 4 o'clock in the morning :)

      philipp

       

Log in to post a comment.