3.4 List serialization

The list (de)serialization mode, which is also the default mode, will store all XML nodes as sequential list items inside the table. The first XML node will be at index 0, the second at index 1, and so on. The table generated by the list deserializer will be a table of tables that contains the whole XML document. The following table fields will be initialized for each node:

Name of the XML node.

Character data of the XML node.

Set to a table of subtables containing all attributes as key and value pairs. Each subtable in the Attrs table will have the following fields initialized:

The attribute's name.

The attribute's value.

Set to a table containing the children of the node.

To print all items of the table deserialized from an XML document using DeserializeTable() you could use the following recursive function:

Function p_PrintNodes(t, indent)
    DebugPrint(RepeatStr(" ", indent) .. "+" .. t.name)
    For Local k = 0 To ListItems(t.attrs) - 1
       DebugPrint(RepeatStr(" ", indent + 1) ..
          "attr: " .. t.attrs[k].key .. "=" .. t.attrs[k].value)
    If Not EmptyStr(t.text)
       DebugPrint(RepeatStr(" ", indent + 1) .. t.text)
    For Local k = 0 To ListItems(t.nodes) - 1
       p_PrintNodes(t.nodes[k], indent + 4)

t = DeserializeTable(FileToString("test.xml"), "xml")
p_PrintNodes(t[0], 0)

To modify a node, you can then simply change the desired value in the Hollywood table and then serialize it back to XML using Hollywood's SerializeTable() function. For example, let's suppose this is your XML file:

<?xml version="1.0"?>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <description>An in-depth look at creating applications
      with XML.</description>

To change the price to 39.95 and save the updated XML to disk, you could do the following:

t = DeserializeTable(FileToString("catalog.xml"), "xml")
t[0].nodes[0].nodes[3].text = "39.95"
StringToFile(SerializeTable(t, "xml"), "catalog_new.xml")

Note that when using the list serializer, the table you pass to SerializeTable() must exactly follow the conventions described above, i.e. the table must contain a number of subtables stored at sequential numeric indices and the fields described above must be initialized for all nodes or there will be an error.

For the example XML shown above, it might be more convenient to use the named serializer, though, because no node names are used twice. See Named serialization for details.

Show TOC