Skip to content

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 specified path (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 to root.
    • indent: (boolean, optional) Whether to indent the output. Defaults to true.
  • 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:

  1. Elements: Become keys in the map.
  2. Attributes: Prefixed with @ (e.g., <user id="1"> becomes {"user": {"@id": 1}}).
  3. Simple Content: If an element only contains text, it becomes a simple value (string, number, or boolean).
  4. Repeated Elements: Elements with the same name at the same level are automatically converted into a slice (array).
  5. Mixed Content: Text content within an element that also has children is stored under the #text key.
  6. Type Conversion: The parser automatically attempts to convert text values to int, float64, or bool if 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]

Released under the ISC License.