FormatXML
You can use the FormatXML function in Optiva Workflows to change the XML declaration encoding and indent XML content for improved readability.
When to use
You can use this function to:
- Format and encode XML as UTF-8 before saving XML to disk, ensuring the encoding matches the file encoding.
- Indent XML content for debugging, logging, or displaying XML to users in a more readable format.
- Convert XML received from external systems, such as UTF-16 XML, to standard UTF-8 encoding for consistency.
When not to use
You cannot use indentation in high-volume or performance-critical scenarios because indentation increases memory and processing overhead.
Syntax
string FormatXML(string XML, short ForceEncoding, bool Indent);
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| XML | string | Yes | The XML string to format |
| ForceEncoding | short | Yes |
Changes the XML declaration node, if available, to the requestedencoding. The possible encoding type values: 0=UTF8 1=Unicode 2=UTF32 3=UTF7 |
| Indent | bool | Yes | true = Indent XML content, false = No indentation |
Return Value
The application returns the formatted and re-encoded XML string if the operation is successful.
Prerequisites
- The XML needs to be well-formed before formatting.
- Indentation increases memory usage.
- No specific permissions are required.
Examples
- Example 1
-
Force UTF-8 Encoding and Indent.
// Get XML from object string xml = ObjectXML(_ObjectSymbol, _ObjectKey, "HEADER;STATUS;PER"); // Force UTF-8 encoding (0) and indent (true) string prettyXML = FormatXML(xml, 0, true);
- Description
- This example shows how to force UTF-8 encoding and indent the XML.
- Example 2
-
Change Encoding from UTF-16 to UTF-8
// Assuming some remote service provided UTF-16 encoded XML string xmlFromRemoteService = "<?xml version=\"1.0\" encoding=\"utf-16\"?><fsxml><formula>..."; // Convert to UTF-8 encoding without indentation string utf8XML = FormatXML(xmlFromRemoteService, 0, false); // Results in: <?xml version="1.0" encoding="utf-8"?><fsxml><formula>...
- Description
- The XML is indented for a better read, but it results in more memory or disk usage. This example shows how encoding is changed for the XML.