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:
Assume this response exists in the %buffer variable. You would read each of the "Student" element within the "Students" root element as follows:
I hope I could help someone.
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.
#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
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.
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
if the file is hug which is containt over 50000k .
it will crash.
11/06/2010 (6:24 am)
i found some bug.if the file is hug which is containt over 50000k .
it will crash.

Associate Phillip O'Shea
Violent Tulip