XML Handlers in DaFT

[DaFT] - [How] - [Install] - [Config] - [JSON & XML] - [Extend] - [Implement]

DaFT provides robust XML handlers for extracting structured data from applications, web APIs, and local files. This guide explains how the XML handlers work and how optional search fields can refine data extraction.

XML Handler Types

DaFT supports three types of XML handlers:

1. App XML (app-xml)

2. Web XML (web-xml)

3. File XML (file-xml)


Using XML Handlers

Each XML handler is defined in config.ini. The following examples demonstrate their configuration:

[example_web_xml]
source       = https://api.example.com/data.xml
type         = web-xml
identifier   = example-api
xml_path     = response.data.items.item
fields       = id,name,score
[example_app_xml]
source       = /usr/local/bin/my-xml-script --option=value
type         = app-xml
identifier   = example-app
xml_path     = results.entry
fields       = id,timestamp
[example_file_xml]
source       = /var/log/data.xml
type         = file-xml
identifier   = example-file
xml_path     = logs.entries.log
fields       = user,action,status

Understanding xml_path

The xml_path setting allows users to extract specific parts of an XML response using dot notation.

Example API Response

<response>
    <status>success</status>
    <data>
        <items>
            <item>
                <id>1</id>
                <name>Alice</name>
                <score>95</score>
            </item>
            <item>
                <id>2</id>
                <name>Bob</name>
                <score>88</score>
            </item>
        </items>
    </data>
</response>

To extract only <data><items>, set:

xml_path = data.items.item

The output would be converted to JSON-like format:

[
  { "id": "1", "name": "Alice", "score": "95" },
  { "id": "2", "name": "Bob", "score": "88" }
]

Using fields to Filter Data

The fields setting allows users to specify which fields should be extracted from the XML structure.

Example Usage

Given the previous <data><items> structure, setting:

fields = id,name

Would produce:

[
  { "id": "1", "name": "Alice" },
  { "id": "2", "name": "Bob" }
]

If a specified field does not exist, it is ignored without causing errors.


Handling Single XML Elements

If the extracted xml_path points to a single XML element, it will be returned as an object instead of an array.

Example XML Response

<response>
    <metadata>
        <info>
            <version>1.0</version>
            <author>Admin</author>
        </info>
    </metadata>
</response>

Configuration

xml_path = metadata.info

Output

{
  "version": "1.0",
  "author": "Admin"
}

What Happens If a Path Doesn’t Exist?

This ensures consistent behaviour and prevents failures due to missing data.


By leveraging XML handlers, DaFT provides efficient and structured data extraction for seamless integration with external services and monitoring systems.