3.3 Serialization interface

If you don't want to use xml.hwp's library interface (see above) for some reason, you can also use the plugin's serialization interface. This is easier to use because it only requires a single function call to convert Hollywood tables to XML documents and vice versa but you won't have fine-tuned control over everything as you have when using the library interface.

Access to the xml.hwp's serialization interface is through Hollywood's SerializeTable() and DeserializeTable() functions, or, alternatively, through the ReadTable() and WriteTable() functions. By using the serialization interface, you can convert an XML document into a Hollywood table through just a single function call:

 
t = DeserializeTable(FileToString("test.xml"), "xml")

The code above will read all nodes and attributes from test.xml and store them in the Hollywood table t.

The way the XML data is stored inside the table depends on the serialization mode you have set using xml.SetSerializeMode(). The XML plugin supports three different serialization modes:

  1. List mode: This 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. This is the default mode. See List serialization for details.

  2. Named mode: This 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. See Named serialization for details.

  3. Hollywood mode: This is a special mode that allows you to serialize arbitrary Hollywood tables. In contrast to the first two modes, Hollywood mode doesn't require the table to follow a certain layout. You can serialize any table in this mode, just as you can with Hollywood's ReadTable() and WriteTable() functions. The table can even contain binary data or code like Hollywood functions. See Hollywood serialization for details.

After you have converted an XML file to a Hollywood table, you could then make any modifications you like directly to the Hollywood table. When you're done with all modifications, you can simply convert your Hollywood table back into an XML document in just a single line like this:

 
StringToFile(SerializeTable(t, "xml"), "test2.xml")

The code above will convert the table t to an XML document using the xml.hwp plugin and save the XML document as test2.xml.

As you can see, the serialization interface is very easy to use but doesn't offer as much flexibility as the library interface which gives you fine-tuned control over many XML documents features.


Show TOC