GameMonkey Script

GameMonkey Script Forums
It is currently Sun Jan 20, 2019 11:21 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: MXML Bind
PostPosted: Thu May 07, 2009 11:57 am 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
Attachment:
MXMLBindGm.zip [2.98 KiB]
Downloaded 329 times


This is my first bind. It works well, but please go over my code and tell me if i'm doing something wrong, mainly the gmUserXMLElement:Trace function as i've havn't got any real idea how it works (i just winged it).

It's a bind for MXML (an XML library, can be found in the downloads section of the GameMonkey website). It can read from an XML file and write out an XML file. To register it:

Code:
RegisterXMLBind(gmMachine* machine);


There's a new type called XMLElement. This is the members for it:
XMLElement.Attributes() - Returns a table of attributes for this element
XMLElement.Children() - Returns a table of child elements for this element
XMLElement.Name - A string which represents the name of the element.
XMLElement.Value - A string which represents the value of the element.

Here's a test XML file:
Code:
<?xml version="1.0" ?>

<Root>
  <Url>www.gmscript.com/gamemonkey/</Url>
 
  <Player>
    <Name>39ster</Name>
    <Power>10</Power>
    <Items>
      <Item name="Sword"/>
      <Item name="Gun"/>
    </Items>
  </Player>

</Root>


So if i wanted to get the value for the first Url element (there is only one Url element) i do:
Code:
//Returns a type called XMLElement
xml = XMLReadFile("xmlfile.xml");

if(xml)
{
  print("Url: " + xml["Url"][0].Value);
}


Notice how there's an index after ["Url"][0]. That index points to which "Url" element we want, as there can be multiple elements of the same name. If you only have one, then you just put 0 as the index.

If we want to write out a new XML file then we do this:
Code:
//Create blank XML element. It will be our root element
xml = XMLElement();
xml.Name = "Root";

//Create a new element that will be a child of the root element.
xmlChild = XMLElement();
xmlChild.Name = "Url";
xmlChild.Value = "www.gmscript.com/gamemonkey/";

//Add the child to the root element
xml.AddChild(xmlChild);

//Write the root and all it's children to a file.
XMLWriteFile("xmlfile.xml", xml);


You can also iterate through the Children. Here's a script that will print out a summary of an XML file (it recursively iterates through all the children):
Code:
global GetXMLAttributes = function(xml)
{
  returnValue = "";
 
  count = 0;
  foreach(name and value in xml.Attributes())
  {
    returnValue += " " + name + "=" + value;
    count += 1;

    if(count < tableCount(xml.Attributes())) {
      returnValue += ",";
    }

  }
  return returnValue;
};

global PrintXMLElement = function(indent, element)
{
  line = "<" + element.name + GetXMLAttributes(element) + ">";
     
  if(element.value.Length()) {
    line += element.value + "</" + element.name + ">";
  }

  print(indent + line);
};

global PrintXMLChildElements = function(indent, xml)
{
  foreach(nodes in xml.Children())
  {
    foreach(element in nodes)
    {
      PrintXMLElement(indent + "  ", element);
      PrintXMLChildElements(indent + "  ", element);
    }
  }
};

xml = XMLReadFile("test.xml");
 
if(xml)
{
  //Print root element
  PrintXMLElement("", xml);

  //Print all children
  PrintXMLChildElements("  ", xml);
}

print("done");


Last edited by 39ster on Tue Jul 27, 2010 4:59 pm, edited 3 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: MXML Bind
PostPosted: Thu May 07, 2009 12:36 pm 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
That looks like a great start 39ster :) I'll try to have a closer look tomorrow. A quick glance at the Trace and Destruct routines looks fine to me. Your code looks neat and readable too, good one!

You can test some things yourself by calling CollectGarbage(true) in script (or equivalent in C++). Look at the memory levels with sysGetMemoryUsage and perhaps some of the sysGetStatsGC* functions. If you are building with MSVC, use a debug build with the CRTDBG enabled and you should be able to see if there are any memory leaks or other anomalies on exit or while running. That should give you confidence that things are fine.


Top
 Profile  
Reply with quote  
 Post subject: Re: MXML Bind
PostPosted: Thu May 07, 2009 3:24 pm 
Offline

Joined: Thu Jan 01, 2004 4:31 pm
Posts: 307
Nice. I'll have a look later.

Can you fill an XMLDoc from a string? Also, it might be nice to convert an XML doc to a flat string as well.


Top
 Profile  
Reply with quote  
 Post subject: Re: MXML Bind
PostPosted: Fri May 08, 2009 5:23 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
I just finished a test and the GC code appears to be operating correctly.
Just a couple of random comments (I realize this is just the start of an interface)...
It would be good to access the error reason for a failed XML file load. Your example .xml file is missing a > after '</Player' which tested this case perfectly.
Depending on the users needs, it may be necessary to preserve the order of child elements, especially those with the same name. A gmTable may prove inadequate (or at least inefficient) to represent this.
Anyway, purely sharing thoughts. Good work :)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group