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
Text
Attrs
Attrs
table will have the following fields initialized:
Key
Value
Nodes
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) Next If Not EmptyStr(t.text) DebugPrint(RepeatStr(" ", indent + 1) .. t.text) EndIf For Local k = 0 To ListItems(t.nodes) - 1 p_PrintNodes(t.nodes[k], indent + 4) Next EndFunction 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"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> </catalog> |
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.