Hi,
I have made a simple XML parsing plugin for AGK. This plugin essentially wraps the TinyXML2 C++ library, however it does not include all of its functionality (more could be added if wanted).
At the moment this plugin has only been compiled for Windows but I should be able to compile for other platforms soon.
Commands:
//Takes a path to the XML file to be loaded
//Returns an Id of the newly loaded XML Document
//If the document failed to load (could not be found or could not be parsed) then -1 is returned
integer LoadXMLDocument(string XML File)
//Gets the sibling element of the current element within the specified document
//Returns an id of the newly discovered element
//if there is no sibling element then -1 is returned
integer GetSiblingElement(integer document id, integer element id)
//Gets the immediate child element of the current element within the specified document
//Returns an id of the newly discovered element
//if there is no child element then -1 is returned
integer GetChildElement(integer document id, integer element id)
//Gets the specified attribute from the element within the document
//Returns a string containing the value of the attribute
//If the attribute does not exist then a blank string is returned
string GetElementAttribute(integer document id, integer element id, string attribute name)
//Gets the name of the specified element in the document
//Returns a string containing the name of the element. NOTE: the name is what is between the < > symbols <NAME HERE>
string GetElementName(integer document id, integer element id)
//Gets the text of the specified element in the document
//Returns a string containing the text within the element NOTE: the what is between the < > </> symbols < NAME HERE> TEXT HERE </NAME HERE>
//If the element contains no text then a blank string is returned
string GetElementText(integer document id, integer element id)
//Gets the element id of the root/first element in the document
//Returns an integer containing the id of the top level element (always 0)
integer GetRootElement(integer document id)
//Releases the specified XML document from memeory
CloseXMLDocument,(integer document)
Here is some test code that goes through all the elements in an XML document and prints out some info about each element:
#import_plugin XMLPlugin as xml
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "PluginTest" )
SetWindowSize( 1280, 720, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1280, 720 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 60, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
SetPrintSize(16)
//Load the xml document
doc = xml.LoadXMLDocument("parent_scene.xml")
do
//Recursive function to print out all of the nodes in the document
getAllElements(doc, xml.GetRootElement(doc))
Sync()
loop
//You should close the XML document when you're done with it
xml.CloseXMLDocument(doc)
//Doc - the XML Document to recurse through
//Ele - the element to start recursing from
Function getAllElements(doc, ele)
//Print out the Element name (<ELEMENT-NAME>) as well as some of the attributes of the element
//If the attribute doesn't exist then a blank string is returned
Print("<" + xml.GetElementName(doc, ele) + " type=" + chr(34) + xml.GetElementAttribute(doc, ele, "type") + chr(34) + " file=" + chr(34) + xml.GetElementAttribute(doc, ele, "file") + chr(34) + ">")
//If the element has any text - print that too
if (Len(xml.GetElementText(doc, ele)) > 0)
Print(xml.GetElementText(doc, ele))
endif
//Get the first child element of this element
nEle = xml.GetChildElement(doc, ele)
//Check if a child exists
if (nEle <> -1)
//A child existed so recurse so we can get
//The child's siblings and children
getAllElements(doc, nEle)
endif
//Check for a sibling element
nEle = xml.GetSiblingElement(doc, ele)
//A sibling exists, so get all of its siblings and children
if (nEle <> -1) then getAllElements(doc, nEle)
Endfunction
Feedback is appreciated, let me know if you have any questions