Xml in UnrealScript

Here’s the translation of the XML example from Go to UnrealScript:

Our first example demonstrates how to work with XML in UnrealScript. UnrealScript doesn’t have built-in XML support, but we can use a third-party library or implement basic XML functionality ourselves. For this example, we’ll create a simple XML structure and demonstrate how to generate and parse XML.

class XMLExample extends Object;

struct Plant
{
    var int Id;
    var string Name;
    var array<string> Origin;
};

function string PlantToString(Plant P)
{
    return "Plant id=" $ P.Id $ ", name=" $ P.Name $ ", origin=" $ JoinArray(P.Origin, ",");
}

function string PlantToXML(Plant P)
{
    local string XML;
    local int i;

    XML = "<plant id=\"" $ P.Id $ "\">\n";
    XML $= "  <name>" $ P.Name $ "</name>\n";
    for (i = 0; i < P.Origin.Length; i++)
    {
        XML $= "  <origin>" $ P.Origin[i] $ "</origin>\n";
    }
    XML $= "</plant>";

    return XML;
}

function Plant XMLToPlant(string XML)
{
    local Plant P;
    local array<string> Lines;
    local string Line;
    local int i;

    Split(XML, "\n", Lines);

    for (i = 0; i < Lines.Length; i++)
    {
        Line = Lines[i];
        if (InStr(Line, "<plant id=") != -1)
        {
            P.Id = int(Mid(Line, InStr(Line, "\"") + 1, InStr(Line, "\"", true) - InStr(Line, "\"") - 1));
        }
        else if (InStr(Line, "<name>") != -1)
        {
            P.Name = Mid(Line, InStr(Line, ">") + 1, InStr(Line, "</name>") - InStr(Line, ">") - 1);
        }
        else if (InStr(Line, "<origin>") != -1)
        {
            P.Origin[P.Origin.Length] = Mid(Line, InStr(Line, ">") + 1, InStr(Line, "</origin>") - InStr(Line, ">") - 1);
        }
    }

    return P;
}

function string GenerateXMLHeader()
{
    return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
}

function XMLExample()
{
    local Plant Coffee, Tomato;
    local string XML;

    // Create a Plant instance
    Coffee.Id = 27;
    Coffee.Name = "Coffee";
    Coffee.Origin[0] = "Ethiopia";
    Coffee.Origin[1] = "Brazil";

    // Generate XML for the Coffee plant
    XML = PlantToXML(Coffee);
    `Log(XML);

    // Add XML header
    `Log(GenerateXMLHeader() $ XML);

    // Parse XML back to a Plant instance
    Coffee = XMLToPlant(XML);
    `Log(PlantToString(Coffee));

    // Create another Plant instance
    Tomato.Id = 81;
    Tomato.Name = "Tomato";
    Tomato.Origin[0] = "Mexico";
    Tomato.Origin[1] = "California";

    // Generate XML for both plants
    XML = "<nesting>\n";
    XML $= "  <parent>\n";
    XML $= "    <child>\n";
    XML $= "      " $ PlantToXML(Coffee) $ "\n";
    XML $= "      " $ PlantToXML(Tomato) $ "\n";
    XML $= "    </child>\n";
    XML $= "  </parent>\n";
    XML $= "</nesting>";

    `Log(XML);
}

defaultproperties
{
}

This UnrealScript example demonstrates basic XML generation and parsing. It defines a Plant structure and provides functions to convert between Plant instances and XML strings.

To use this example:

  1. Create a new UnrealScript file named XMLExample.uc in your project’s Classes directory.
  2. Copy the code into the file.
  3. Compile the script as part of your Unreal project.
  4. You can then create an instance of XMLExample in your game code or use it in the UnrealScript console to see the XML output.

Note that this is a simplified example and doesn’t cover all aspects of XML parsing and generation. For more complex XML operations, you might want to consider using a third-party XML library compatible with UnrealScript or implementing a more robust XML parser.