Skip to main content

XML

Parse

Synopsis

Parses XML-formatted strings into structured maps.

Schema

xml:
- field: <ident>
- target_field: <ident>
- description: <text>
- if: <script>
- add_to_root: <boolean>
- ignore_failure: <boolean>
- ignore_missing: <boolean>
- on_failure: <processor[]>
- on_success: <processor[]>
- tag: <string>

Configuration

FieldRequiredDefaultDescription
fieldYField containing the XML string to parse
target_fieldNField to store the parsed XML structure. If omitted and add_to_root is false, an error is raised
descriptionN-Explanatory note
ifN-Conditional expression to determine if processing should occur
add_to_rootNfalseIf true, adds parsed XML elements directly to the log entry root
ignore_failureNfalseSkip processing if an error occurs
ignore_missingNfalseSkip processing if the source field is missing
on_failureN-Processors to run if processing fails
on_successN-Processors to run after successful processing
tagN-Identifier for logging purposes

Details

The processor converts XML documents into nested map structures, handling complex scenarios like nested elements, attributes, and mixed content.

Attributes are stored in a special _attributes map within the element. Text content is stored under:

  • _text for elements with attributes
  • _content for elements without attributes

For mixed content, such as nested structures, both the layout and the text is preserved. Repeated elements are automatically converted to arrays.

warning

Invalid XML will cause processing to fail unless ignore_failure is set to true.

Examples

Basic

Parsing a simple XML structure...

{
"xml_data": "<person><name>John</name><age>30</age></person>"
}
xml:
- field: xml_data
- target_field: parsed_data

creates a structured map:

{
"parsed_data": {
"person": {
"name": "John",
"age": "30"
}
}
}

Attributes

Parsing XML with element attributes...

{
"xml_data": "<user id=\"123\" type=\"admin\"><name>Alice</name></user>"
}
xml:
- field: xml_data
- target_field: parsed_data

handles each separately:

{
"parsed_data": {
"user": {
"_attributes": {
"id": "123",
"type": "admin"
},
"name": "Alice"
}
}
}

Repeated Elements

Parsing XML with repeated elements...

{
"xml_data": "<books><book>Book1</book><book>Book2</book></books>"
}
xml:
- field: xml_data
- target_field: parsed_data

converts repeated elements to arrays:

{
"parsed_data": {
"books": {
"book": ["Book1", "Book2"]
}
}
}

Adding to Root

Adding parsed XML directly to log entry root...

{
"xml_data": "<data><value>test</value><id>123</id></data>"
}
xml:
- field: xml_data
- add_to_root: true

adds the elements directly:

{
"data": {
"value": "test",
"id": "123"
}
}

Complex Nested

Parsing complex XML with nested elements and attributes...

{
"xml_data": "<library><category name=\"fiction\"><book id=\"1\"><title>Book1</title><authors><author>Author1</author><author>Author2</author></authors></book></category></library>"
}
xml:
- field: xml_data
- target_field: parsed_data

preserves the layout and attributes:

{
"parsed_data": {
"library": {
"category": {
"_attributes": {"name": "fiction"},
"book": {
"_attributes": {"id": "1"},
"title": "Book1",
"authors": {
"author": ["Author1", "Author2"]
}
}
}
}
}
}