Game Development Community

XML Question

by Matt Huston · in Torque Game Builder · 04/10/2009 (5:37 pm) · 4 replies

I am using the basic xml loading that is provided in TGB via the functions in xml.cs.

My question is how would I read through several fields that all have the same name? I have tried using a for loop to read through the fields but it only reads the very first field so I get "Mix all ingredients together" 3 times.

<stepCount>3</stepCount>
<instructions>
     <step>Mix all ingredients together.</step>
     <step>Knead thoroughly.</step>
     <step>Cover with a cloth, and leave for one hour in warm room.</step>
</instructions>

if( %xml.beginRead( %filename ) )
   {
      %stepCount  = %xml.readField( "stepCount" );
      
      for(%i = 0 ; %i < %stepCount; %i++)
      {
         if( %xml.readClassBegin( "instructions", %i ) )
         {
               %step = %xml.readField( "step" );

               echo(%step);
               
               %xml.readClassEnd();
         }
      }

      %xml.endRead();
   }

#1
04/10/2009 (5:55 pm)
Thought I cracked this but was not meant to be. I moved the readClassBegin out of for loop.

if( %xml.beginRead( %filename ) )
{
   %stepCount  = %xml.readField( "stepCount" );
  
   if( %xml.readClassBegin( "instructions") )
   {
      for(%i = 0 ; %i < %stepCount; %i++)
      {
         %step = %xml.readField( "step" );
      }
      %xml.readClassEnd();
   }

   %xml.endRead();
}
#2
12/08/2009 (3:16 am)
i need the same functionality...did you get it to work?

do you know what the '%index' attribute in readClassBegin is? saw that readField has only 2 defined attributes, (%this, %filename)...so how are you supposed to get multiple elements?
#3
12/08/2009 (4:37 am)
by reading field after field.

As a function only can return a single value at a time, you need multiple reads to the xml node anyway
#4
12/08/2009 (5:17 am)
<instructions>
<step>Mix all ingredients together.</step>
<step>Knead thoroughly.</step>
<step>Cover with a cloth, and leave for one hour in warm room.</step>
</instructions>


%step1 = %xml.readField( "step" );
%step2 = %xml.readField( "step" );
%step3 = %xml.readField( "step" );

if i 'readField' thrice, am still going to get 'Mix all ingrediants together' in %step1, %step2 and %step3...there's nothing like readField("step", %elementIndex)