XML Services (system.xml)
This namespace provides services for parsing and manipulating XML data structures.
Services
system.xml:Parse
Parses an XML string into a structured map with automatic type conversion for numbers and booleans.
- Inputs:
xml: (string) The raw XML string.path: (string, optional) Dot-notation path to extract a specific value after parsing.
- Outputs:
parsed: (object) The fully parsed XML object.value: (any) The value at the specifiedpath(if provided).
system.xml:Extract
Extracts a value from a structured object (map or slice) using dot notation.
- Inputs:
data: (object) The structured object to extract from.path: (string) Dot-notation path (e.g.,Root.User.Name).
- Outputs:
value: (any) The value found at the specified path.
system.xml:Format
Converts a structured map into an XML string.
- Inputs:
data: (object) The data to convert.rootName: (string, optional) The name of the root element. Defaults toroot.indent: (boolean, optional) Whether to indent the output. Defaults totrue.
- Outputs:
xml: (string) The formatted XML string.
XML to Map Convention
Since XML has a different structure than JSON, the following conventions are used when converting to a map:
- Elements: Become keys in the map.
- Attributes: Prefixed with
@(e.g.,<user id="1">becomes{"user": {"@id": 1}}). - Simple Content: If an element only contains text, it becomes a simple value (string, number, or boolean).
- Repeated Elements: Elements with the same name at the same level are automatically converted into a slice (array).
- Mixed Content: Text content within an element that also has children is stored under the
#textkey. - Type Conversion: The parser automatically attempts to convert text values to
int,float64, orboolif they match the standard format.
Example
XML:
xml
<root version="1.0">
<user id="101">
<name>John Doe</name>
<role>Admin</role>
<role>Editor</role>
</user>
</root>Resulting Map:
json
{
"root": {
"@version": 1.0,
"user": {
"@id": 101,
"name": "John Doe",
"role": ["Admin", "Editor"]
}
}
}Path Syntax
- Nesting:
root.user.name - Attributes:
root.user.@id - Arrays:
root.user.role[0]
