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.
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();
}About the author
www.atomicbanzai.com
#2
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?
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
As a function only can return a single value at a time, you need multiple reads to the xml node anyway
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
<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)
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)
Torque 3D Owner Matt Huston
Atomic Banzai Games
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(); }