3.5 Named serialization

The named (de)serialization mode will store all XML nodes as named items inside the table. This means that you can conveniently access nodes by their name instead of having to use numeric indices. The disadvantage of this mode is that you can't have multiple nodes of the same name on the same level because the nodes are stored by name and each name is only available once per node level. Another disadvantage is that you don't have control over the order of the nodes when serializing them back to an XML document.

The following table fields will be initialized for each node when using the named serialization mode:

Text
Character data of the XML node.

Attrs
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:

Key
The attribute's name.

Value
The attribute's value.

Nodes
Set to a table containing the children of the node.

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:

 
xml.SetSerializeMode(#XML_SERIALIZEMODE_NAMED)
t = DeserializeTable(FileToString("catalog.xml"), "xml")
t.catalog.nodes.book.nodes.price.text = "39.95"
StringToFile(SerializeTable(t, "xml"), "catalog_new.xml")

Note that when using the named 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 named indices and the fields described above must be initialized for all nodes or there will be an error.

Also note that since the named serializer stores XML nodes as named table fields, you can only use the same name once per tree level. Thus, you can't use the named serializer to deserialize XMLs that have multiple nodes of the same name, e.g. like this:

 
<?xml version="1.0"?>
<catalog>
   <item>First book</item>
   <item>Second book</item>
   <item>Third book</item>
</catalog>

In the XML shown above, there are three <item> tags on the same tree level. This can't be deserialized with the named serializer because in Hollywood tables each name can only be used once. To deserialize such tables you have to use the list serializer. See List serialization for details.


Show TOC