Using XML

The following are example code snippets and simple instructions to help users get started with the Scaleform XML implementation.

Enabling ActionScript XML Support

To enable ActionScript XML processing support, the XML parser state needs to be set for the Scaleform loader during Scaleform initialization. The following is an excerpt from the default FxPlayer.cpp file. The same pattern can be used with a custom parser implementation:

…
#include "XML/XML_Expat.h"
using namespace Scaleform;
…
Ptr<XML::Parser> pexpatXmlParser = *new XML::ParserExpat;
Ptr<XML::SupportBase> pxmlSupport = *new XML::Support(pexpatXmlParser);
mLoader.SetXMLSupport(pxmlSupport);…

Document Parsing from C++

The parser state can be used to parse XML documents directly from C++. If ActionScript XML support is enabled, the following can be used to create a DOM tree from an XML file (This requires access to full Scaleform source):

…
#include "GFx/XML/XML_DOM.h"
using namespace Scaleform;
…
// Create a DOM builder that processes whitespace
XML::DOMBuilder domBuilder(Loader.GetXMLSupport());
// OR create a DOM builder that ignores whitespace
XML::DOMBuilder domBuilder2(Loader.GetXMLSupport(), true);
…
// Process the xml file and return the root of the DOM tree (creates an
// object manager internally)
Ptr<XML::Document> pdoc = domBuilder.ParseFile("inputfile.xml",
   mLoader.GetFileOpener());
// OR process the xml file and return the root of the DOM tree (use provided object manager)
Ptr<XML::ObjectManager> pobjMgr = *new XML::ObjectManager();
Ptr<XML::Document> pdoc2 = domBuilder.ParseFile("inputfile.xml",
mLoader.GetFileOpener(), pobjMgr);
…

If ActionScript XML support is not enabled, a stand-alone instance of the parser can be created and used as follows:

…
#include "GFx/XML/XML_DOM.h"
#include "GFx/XML/XML_Expat.h"
using namespace Scaleform;
…
Ptr<XML::Parser> pexpatXmlParser = *new XML::ParserExpat;
Ptr<XML::Support> pxmlSupport = *new XML::Support(pexpatXmlParser);
…
// Create a DOM builder that processes whitespace
XML::DOMBuilder domBuilder(pxmlSupport);
// Create a DOM builder that ignores whitespace
XML::DOMBuilder domBuilder2(pxmlSupport, true);
…
// Same as processing from previous section
…

Traversing the DOM Tree in C++

The following code prints out the contents of a DOM tree (This requires access to full Scaleform source):

…
using namespace Scaleform;
…
void PrintDOMTree(XML::Node* proot)
{
    switch (proot->Type)
    {
        case XML::ElementNodeType:
        {
            XML::ElementNode* pnode = static_cast< XML::ElementNode*>(proot);
            if (pnode->Prefix.GetSize() > 0)
            {
                printf("ELEM - '%s:%s' ns:'%s' prefix:'%s'" " localname: '%s'",
                        pnode->Prefix.ToCStr(),
                        pnode->Value.ToCStr(),
                        pnode->Namespace.ToCStr(),
                        pnode->Prefix.ToCStr(),
                        pnode->Value.ToCStr());
            }
            else
            {
                printf("ELEM - '%s' ns:'%s' prefix:''" " localname: '%s'",
                        pnode->Value.ToCStr(),
                        pnode->Namespace.ToCStr(),
                        pnode->Value.ToCStr());
            }
            for (XML::Attribute* attr = pnode->FirstAttribute;
                attr != NULL; attr = attr->Next)
            {
                printf(" {%s,%s}", attr->Name.ToCStr(), attr->Value.ToCStr());
            }
            printf("\n");
            for (XML::Node* child = pnode->FirstChild; child != NULL;
                child = child->NextSibling)
            PrintDOMTree(child);
            break;
        }
        case TextNodeType:
        {
                printf("TEXT - '%s'\n", proot->Value.ToCStr());
                break;
        }
        default:
        {
                printf("UNKN\n");
        }
    }
}
…
Ptr<XML::Document> pdoc = domBuilder.ParseFile("inputfile.xml",  
   Loader.GetFileOpener());
PrintDOMTree(root);

Configuring Debug Reporting

Users who have access to the Scaleform source files are able to set/unset flags in XML_DOM.cpp to specify the XML parsing, and DOM construction reporting flags.

//
// Debug Output & Tracing Flags
//
#ifdef SF_BUILD_DEBUG
//
// Trace the document builder's DOM tree construction in debug output
//
// #define SF_XML_DOCBUILDER_TRACE
//
// Dump all constructed DOM trees to standard out
// (Warning: Avoid this for large files)
//
// #define SF_XML_DOCBUILDER_DOM_TREE_DUMP
//
#endif  // #ifdef SF_BUILD_DEBUG