The two actions, Load from String and Save to String, respectively:
•load JSON or XML data from a string into a JSON/XML page source, and
•serialize a JSON/XML page source to a string, and save the serialized string to a location specified by an XPath expression.
These functions are particularly useful in Embedded Webpage Solutions, where serialized XML data can be received in a message from a webpage and stored in the $MT_EMBEDDEDMESSAGE JSON page source. The Load from String action can take the XML string and generate an XML page source. Conversely, an XML page source can be serialized to a string with the Save to String action and stored in a page source node.
Load from String
The Load from String action parses the string that is targeted by the XPath expression of the Source Node setting (see screenshot below), and generates the structure and data of the selected page source (see screenshot). Select the page source from the Page Source combo box, or an XPath expression that locates the root node of the page source (for example: $XML1).
The type of the page source selected (JSON or XML) should correspond to the serialization of the string. So, if the source node string is a JSON string, then a JSON document will be generated. If the source node string is an XML string, then this string is loaded into the page source, which must be an XML page source; the root element in the serialized string will be created as the root element of the XML page source.
•About the source string: In the screenshot above, an XML string is located in the source node $SERIALIZEDSTRINGS/Strings/XML_String. This is the string that will be loaded into the selected page source. In order for this to work correctly, the page source must be an XML page source. (The XPath expression of the Source Node setting need not take a string from a node; the string can also be entered directly in the XPath expression.)
•Encoding of the source string: The following encodings are supported on clients, so the source string must be in one of these encodings: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE, US-ASCII, ISO 8859-1.
•About the JSON/XML page source: The page source needs to have been created before it is selected in the action. The JSON/XML document that is loaded from the string is created as the entire page source within the document node. If, at run time, the structure of the loaded document does not match the structure of the page source as defined in the design, then the solution will not be executed correctly. This is because the design works with the node names of an expected page source structure, but the nodes of the page source that is actually created have different names.
Save to String
The Save to String action (screenshot below) serializes the page source named in the Page Source option, and saves the serialized string to the location that is specified by the XPath expression of the Target Node setting. You can also select the page source via the setting's combo box or via an XPath expression that locates the root node of the page source (for example: $XML1).
Note the following points:
•The page source and its structure must exist at design time so that it can be selected as a combo box option.
•The content of the entire page source, no matter of what type, will be serialized, from its first character to its last character, to a string.
•The string that results from the serialization will be saved to the node specified in the XPath expression.
An example file named LoadSaveString.mtd demonstrates the usage of these two actions. This file is located in the (My) Documents folder: Altova\MobileTogetherDesigner6\MobileTogetherDesignerExamples\Tutorials\Actions. This example contains two page sources ($JSON1 and $XML1), with each containing, respectively, fixed JSON and XML data (see screenshot). By using the Save to String action, each page source can be serialized as a string and saved to a node. We then reverse this action by using the Load from String action to load the just-serialized strings into new page sources ($JSONFROMSTRING and $XMLFROMSTRING).
•The design file contains a JSON page source ($JSON1) and XML page source ($XML1), the structures and data of which are defined in the file itself (see screenshot of simulation above). Both data structures are rudimentary.
•The design contains a button to respectively save each page source as a serialized string (Save JSON/XML String), each in a different node of another page source named $SERIALIZEDSTRINGS. Both these buttons use the Save to String action.
•The generated serializations are used to create new pages sources, respectively, $JSONFROMSTRING and $XMLFROMSTRING. This is done by triggering the Load from String actions of the third and fourth buttons (Load JSON/XML String).
This example, in effect, uses the two actions (Save to String and Load from String) to make a round trip from one page source to another page source via a serialized string. The origin and destination page sources will have the same structure and data.