
1. Creating Complex XMLs Using the XML/JSON Development Tool
Crafting complex XML documents, often characterized by attributes in most nodes, can be a challenging task. Typically, in such documents, data is embedded in attributes rather than within XML tags. The XML/JSON development tool simplifies this process considerably.
- Creating Complex XMLs Using the XML/JSON Development Tool
- Utilizing the Development Tool for Parsing Data
Understanding XML Attributes
In standard XML, data values are usually placed between a start tag and an end tag. However, attributes are defined within the start tag itself as key/value pairs, instead of being enclosed between tags.
Example:
<Data ss:Type="String">WebShop orders</Data>
Here:
ss:Type="String"
is an attribute.- "WebShop orders" is the data value.
Prettifying XML Documents
Working with sample XML documents is a recommended technique. If the sample is not formatted (i.e., the content is on a long, unindented line), you can 'prettify' it. Online tools are available for this purpose; simply search for "XML prettify online" to find several options.
Alternatively, Mac users can use the xmllint
command-line tool:
$ cd Desktop
$ xmllint --format myUnprettyXML.xml > myPrettyXML.xml
$
After prettifying, the document can be opened in a plain text editor like TextMate or BBEdit.
1.1. Creating XML Snippets from a Complex Document ↑
When you open a prettified XML template in a text editor, it's easy to copy valid XML snippets, including their end tags, to avoid parsing errors. For example:
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="12" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
In the development tool, this snippet translates to the following ACF code:
Function Excel_DefaultStyle()
XML xmlVar;
xmlVar["Style"] =
XML("Alignment","","Borders","","Font","",
"Interior","","NumberFormat","","Protection","");
// Assign attributes to the XML...
XMLattributes(xmlVar["Style"], XML("ss:ID","Default","ss:Name","Normal");
XMLattributes(xmlVar["Style.Alignment"], XML("ss:Vertical","Bottom");
XMLattributes(xmlVar["Style.Font"], XML("ss:FontName","Calibri","x:Family","Swiss","ss:Size","12",
"ss:Color","#000000");
return XmlVar;
End
You can easily replace any hardcoded values with function parameters for dynamic content creation.
Creating a Style Catalog
To create a style catalog, you can append multiple styles to the "Styles" tag. For example:
XML styles;
styles["Styles"] = Excel_DefaultStyle();
styles["Styles+"] = Excel_BoldTextStyle();
The "+" sign indicates appending a new style instead of replacing the existing one. This differs from the double square bracket notation, which would create multiple separate "styles" tags. The "+" variant is the correct approach in this context.
(Note: The "+" feature was introduced in plugin version 1.7.0.7.)
1.2. Clearing XML Variables Before Re-use ↑
Use the clear(variable)
command to reset an XML variable, especially useful in loops for building structures. For instance:
int i, lines = sizeof(Ordre_nr);
XML row;
for (i = 1, lines) {
clear(row);
// Add cells to the row...
row["Row+"] = Excel_TextCell(string(Ordre_nr[i]), "Default");
row["Row+"] = Excel_TextCell(string(Firma_ID[i]), "Default");
row["Row+"] = Excel_TextCell(l_Firma_navn[i], "Default");
...
...
excel["Workbook.Worksheet.Table+"] = row;
NumberOfRows++;
}
clear(row);
This approach ensures each new row in the loop is fresh and uncluttered, ready for new data.
2. Utilizing the Development Tool for Parsing Data ↑
Efficient XML Parsing from Web Service Responses
When you receive an XML response from a web service and need to parse its data, our development tool can significantly streamline the process. It allows you to generate ACF code that replicates the structure of the received XML, facilitating easy parsing and data manipulation.
Convenient Code Generation for Custom Use
Instead of using the functions directly, consider them as a rich source for copy-and-paste operations. The tool generates functions with all the necessary paths, making it easier to extract and work with the specific data segments you need.
Handling Variable Web Service Responses
Web services often return different XML documents based on various states – a standard response, an error message, or variations tied to the specific nature of the requested data.
Intelligent Response to Non-Existent Nodes
Our tool has a unique approach to handling requests for values in non-existent nodes. Instead of returning an empty string – which could be confused with an empty node – it returns a question mark (“?”). This distinction allows for a more accurate interpretation of the web service response.
Example: Checking the Status of a Web Request
XML resp = HTTP_GET(.......);
if (resp["Data.Status.ErrorMessage"] == "?") {
// Operation successful: No error message present in the response
}
In this example, we use the response data to check if there’s an error message. The absence of an error (indicated by “?”) confirms that the request was processed successfully.