OpenViSTAI · Agents · Blockchain · Crypto
Menu
OPENVIST RECIPES

Inspect Agent Workflow Nodes in a Local XML File

Python

Read a saved XML workflow file and print the tag names and attributes of elements selected by an exact tag name, without executing or modifying the workflow.

Python code excerpt: Inspect Agent Workflow Nodes in a Local XML File. Version 0.1.0. Read the full recipe for code, sources and recorded test scope.

Workflow exports can contain configuration and action-like nodes that should be inspected as data, not run. This Python 3.12 CLI parses a user-supplied XML file and emits a JSON list of matching element tags and attributes. It is a local XML inspection tool, not a test of any agent runtime or blockchain service.

Before you start

  • Intended runner: Python 3.12; no tested minimum Python version is claimed.
  • Only the Python standard-library argparse, pathlib, sys, json, and xml.etree.ElementTree modules are used.
  • Input is a local UTF-8 XML file and an exact, nonblank element tag argument; there are no credentials or external services.
  • The code reads the complete file before parsing. It specifies no timeout because it makes no network calls.

Build and check

  1. Save the program as `inspect_workflow.py`.
  2. Create a synthetic local XML fixture containing matching and nonmatching elements, including action-like elements with harmless attributes.
  3. Run `python3.12 inspect_workflow.py fixture.xml action` and check that only matching elements appear, in document traversal order.
  4. Run `python3.12 inspect_workflow.py fixture.xml missing-tag` and check that the output is an empty JSON array.
  5. Try an empty file and malformed XML; verify the tool reports a parse error to stderr and exits with status 2.
  6. Try a nonexistent path and a non-UTF-8 fixture; verify a clear input error is sent to stderr with status 2.

Limits to understand

  • ElementTree is documented as not secure against maliciously constructed XML. Use only trusted, appropriately bounded local fixtures; this tool is not a hardened parser for hostile XML.
  • The complete input is loaded into memory before parsing, so it is intended for small files, not large exports.
  • The tag match is exact and namespace-qualified names must be supplied in ElementTree's expanded-name form when applicable.
  • The output contains attributes only; it does not validate workflow schemas, establish what an agent runtime would do, or execute any action.
  • An empty file is not a well-formed XML document and produces a parse error; a valid document with no matching tag yields `[]`.

What this example adds

This is a new offline inspection task, not a variant of an existing task: it traverses an XML tree by an exact tag and exposes only each matching element's tag and attributes as JSON. It does not invoke workflow actions, interpret element text as instructions, or connect to an agent or blockchain service.

Run it

Save the code as `inspect_workflow.py`, then run `python3.12 inspect_workflow.py workflow.xml action`. The file is read locally as UTF-8; output is written to stdout and diagnostics to stderr. No network call or retry is made.

Example input

<workflow><action name="read-state" mode="preview"/><action name="submit" mode="disabled"/><note>Do not execute this file.</note></workflow>

Expected output

[{"attributes": {"mode": "preview", "name": "read-state"}, "tag": "action"}, {"attributes": {"mode": "disabled", "name": "submit"}, "tag": "action"}]
INSPECT THE SCRIPT

Inspect, copy and run the documented checks.

This browser preview builds code locally. It does not contact an API or run the code.

Default example ready.

Python example
# SPDX-License-Identifier: MIT
# Copyright (c) 2026 OpenViST contributors
import argparse
import json
import pathlib
import sys
import xml.etree.ElementTree as ET


def main():
    parser = argparse.ArgumentParser(
        description="Inspect matching elements in a local XML file without running it."
    )
    parser.add_argument("xml_file", help="local XML file to inspect")
    parser.add_argument("tag", help="exact element tag name to select")
    args = parser.parse_args()

    if not args.tag.strip():
        parser.error("tag must not be blank")

    try:
        text = pathlib.Path(args.xml_file).read_text(encoding="utf-8")
    except (OSError, UnicodeError) as exc:
        print(f"input error: {exc}", file=sys.stderr)
        return 2

    try:
        root = ET.fromstring(text)
    except ET.ParseError as exc:
        print(f"XML parse error: {exc}", file=sys.stderr)
        return 2

    matches = [
        {"tag": element.tag, "attributes": dict(element.attrib)}
        for element in root.iter(args.tag)
    ]
    print(json.dumps(matches, ensure_ascii=False, sort_keys=True))
    return 0


if __name__ == "__main__":
    sys.exit(main())
What to verify after running
  • Run the synthetic matching case and verify the JSON is a list containing the two `action` nodes and their attributes, with no element text.
  • Run with a tag absent from a valid fixture and verify stdout is exactly `[]`.
  • Run against a malformed XML fixture and verify a parse diagnostic, nonzero status 2, and no JSON result.
  • Run with a missing path and with invalid UTF-8 input; verify each fails clearly rather than silently producing partial output.
  • Review the fixture to ensure its action-like contents are inert sample data; the program must only parse and print selected structure.

Request or format preview

Illustrative preview. No provider response, price or balance is fetched.

Change a setting to inspect the preview.

Official sources and related profiles

    Code license: MIT. Keep the license notice with your copy.

    Example version 0.1.0 · Documentation reviewed 2026-10-12.

    Executed 6 synthetic local fixture cases in isolated CPython 3.12.15 on GitHub Actions, without network access. Declared exit codes, output assertions and input integrity checks passed. Live services, installed third-party packages and production behavior remain outside scope. Editorial approval is still required.