blog games developers documentation portfolio gallery

More in this category:

Import()

To import any XML, you can call the class methods of SimpleXmlImporter.

// import an XML string
myHashtable = SimpleXmlImporter.Import(xmlString);

As easy as that.

The class methods are:

Hashtable Import(string xml)


Simply import an xml string hierarchy. The import is case sensitive and the full file will be read

Hashtable Import(string xml, bool caseInsensitive)


if caseInsensitive is true, all tag names will be converted to lowercase. If false, the result is the same as the function Hashtable Import(string xml)

Hashtable Import(string xml, string tagName)


Will only read the first occurence of the tag named tagName in the XML string as if this is the top level element. If tagName = null the result is the same as importing the whole string

Hashtable Import(string xml, string tagName, bool caseInsensitive)


if caseInsensitive is true, all tag names will be converted to lowercase. If false, the result is the same as the function Hashtable Import(string xml, string tagName)
Will only read the first occurence of the tag named tagName in the XML string as if this is the top level element. If tagName = null the result is the same as importing the whole string.

Import examples


Importing XML into Hashtables and ArrayLists sounds simple enough, but there are a few caveats. Let's look at a few examples.
First a very simple XML string:
<level1>
<level2>
Somevalue
</level2>
</level1>

Importing it gives:
{ // Hashtable
level1 : { // Hashtable
level2 : Somevalue,
}
}


This time with an extra property:
<level1>
<level2a>
Somevalue
</level2a>
<level2b>
Anothervalue
</level2b>
</level1>

Imported:
{ // Hashtable
level1: { // Hashtable
level2a: Somevalue,
level2b: Anothervalue,
},
}


Here level1 contains an ArrayList instead of a Hashtable:
<level1>
<level2>
Somevalue
</level2a>
<level2>
Anothervalue
</level2>
</level1>

Imported:
{
level1: [
{
level2: Somevalue,
},
{
level2: Anothervalue,
},
],
}

Exported back to XML:
<level1>
<level2>Somevalue</level2>
<level2bgt;Anothervalue</level2>
</level1>


This is all fine, but ArrayLists have the disadvantage of nor being able to store keys for their elements. Check out what happens here:
<level1>
<level2>
<level3a>1</level3a>
<level3b>2</level3b>
<level3c>3</level3c>
</level2>
<level2>
<level3a>4</level3a>
<level3b>5</level3b>
<level3c>6</level3c>
</level2>
<level2>
<level3a>7</level3a>
<level3b>8</level3b>
<level3c>9</level3c>
</level2>
</level1>

Imported:
{
level1: [
{
level3a: 1,
level3b: 2,
level3c: 3,
.tag.: level2,
},
{
level3a: 4,
level3b: 5,
level3c: 6,
.tag.: level2,
},
{
level3a: 7,
level3b: 8,
level3c: 9,
.tag.: level2,
},
],
}

All of a sudden we get a new key in the level2 hashtables, that identifies the original tag. Without it, the tag name "level2" would have gotten lost and it would be impossible to export it back to the original XML.

But it gets more weird. XML also supports properties inside the tags. <tag property="value">. And they suck big time. Look what happens:
<level1>
<level2 property:"123">
Somevalue
</level2>
<level2 property:"456">
Anothervalue
</level2>
</level1>

Imported:
{
level1: [
{
.tag.: level2,
level2: Somevalue,
property: 123,
},
{
.tag.: level2,
level2: Anothervalue,
property: 456,
},
],
}

Exported back to XML:
<level1>
<level2 property="123">
Somevalue
</level2>
<level2 property="456">
Anothervalue
</level2>
</level1>


The same thing happens here. Unless the importer adds an extra key called ".tag." to store the original tag name, the information would get lost.

In the previous example level1 contained an arraylist of level2's. When another key would be added to level1, all of a sudden it is no longer able to identify its members. Like this::
<level1>
<level2a property:"123">
Somevalue
</level2a>
<level2a property:"456">
Anothervalue
</level2a>
<level2b>I will mess things up</level2>
</level1>

Level 1 can no longer contains an arraylist, as it would be unable to make the difference between its level2a and level2b members. And so it becomes a hashtable instead, that contains an array list for level2a. Like so:
Imported:
{
level1: {
level2a: [
{
.tag.: level2a,
level2a: Somevalue,
property: 123,
},
{
.tag.: level2a,
level2a: Anothervalue,
property: 456,
},
],
level 2b: "I will mess things up",
},
}







follow us