Game Development Community

dev|Pro Game Development Curriculum

Reading XML in TorqueScript (TGB)

by Mario Di Vece · 08/15/2009 (11:17 am) · 9 comments

Hi all. This is my first post in TDN. As I was unable to find good documentation on reading XML files, here's an intro on how you can do that.

Assume you create an HTTPObject request and you get the corresponding XML response. The XML response looks as follows:

<Students xmlns="http://schemas.myschool.net/Students/2009/08">
    <Student studentId="1" firstName="Luis" lastName="Gonzalez"/>
    <Student studentId="2" firstName="Geovanni" lastName="Perez"/>
    <Student studentId="3" firstName="Victor" lastName="Mendoza"/>
</Students>

Assume this response exists in the %buffer variable. You would read each of the "Student" element within the "Students" root element as follows:

%xml = new SimXMLDocument() {};
%xml.parse(%buffer);

// "Get" inside of the root element, "Students".
%xml.pushChildElement(0);

// "Get" into the first child element 
if (%xml.pushFirstChildElement("Student"))
{
    while (true)
    {
        /* 
         * Here, i read the element's attributes. 
         * You might want to save these values in an array or call the %xml.getElementValue()
         * if you have a different XML structure.
         */
        %studentId = %xml.attribute("studentId");
        %firstName = %xml.attribute("firstName");
        %lastName = %xml.attribute("lastName");
        // now, read the next "Student"
        if (!%xml.nextSiblingElement("Student")) break; 
    }
}

I hope I could help someone.

About the author

Unosquare.com

Recent Blogs


#1
08/15/2009 (2:47 pm)
There is a script class floating around which makes it pretty easy to read and write XML docs via TorqueScript. It is aptly named "xml.cs" and it should be in the tools folder of TGE/A or the common folder in TGB.
#2
08/16/2009 (3:02 am)
Nonetheless, great first blog, Mario!
#3
08/16/2009 (8:15 pm)
Is this only with TGB or will this work with T3D?
#4
08/17/2009 (9:45 pm)
@Kevin: SimXMLDocument also exists in Torque3D
#5
08/18/2009 (3:28 pm)
Thanks so much for posting this!

I've been developing things in Flash, and got very comfortable making XML data files for my projects since Flash hardly reads anything else. I'm sort of new to TGB, and it's great to know I can continue using XML for my Torque projects.
#6
08/26/2009 (9:42 pm)
Just in case anyone coms across this article and is looking for how to open an XML file, I have added my working T3D code.

function getModelsInCatagory()
{ 
   %file = "./Catalog.xml";
   %fo = new FileObject();
   %text = "";

   if(%fo.openForRead(%file))
   {
      while(!%fo.isEOF())
      {
         %text = %text @ %fo.readLine();
         if (!%fo.isEOF()) %text = %text @ "\n";
      }
   }
   else
   {
      echo("Unable to locate the file: " @ %file);
   }

   %fo.delete();
   
   %xml = new SimXMLDocument() {};   
   %xml.parse(%text);   
  
   // "Get" inside of the root element, "Students".   
   %xml.pushChildElement(0);   
  
   // "Get" into the first child element    
   if (%xml.pushFirstChildElement("Model"))   
   {   
      while (true)   
      {   
         /*   
         * Here, i read the element's attributes.   
         * You might want to save these values in an array or call the %xml.getElementValue()  
         * if you have a different XML structure.  
         */  
         %catagory = %xml.attribute("catagory");   
         %name = %xml.attribute("name");   
         %path = %xml.attribute("path");   
         
         // now, read the next "Model"   
         if (!%xml.nextSiblingElement("Model")) break;    
      }   
   }
}
#7
11/03/2010 (10:29 am)
it is quite good for me :)
#8
11/06/2010 (6:24 am)
i found some bug.
if the file is hug which is containt over 50000k .
it will crash.
#9
01/18/2012 (9:44 am)
All,

OK, 3.5 years after the fact, is XPath supported?