🔍

xml.o

PEG-based XML parser that processes XML documents and converts them into nested dictionary structures. The parser handles XML elements, attributes, text content, and comments.

Usage

o)load[getenv[`OHOME];"xml"];

Main elements

Name Description / comments
.xml.parsed[<xml string>] Parse XML string and return nested dictionary with element structure

Parsed structure

The parser returns a dictionary where each XML element contains:

  • Element name as dictionary key (symbol)
  • children - list of child elements or text nodes
  • attrs - dictionary of element attributes (if present)
  • text - text content (for text nodes)

Examples

Basic XML parsing:

o)load[getenv[`OHOME];"xml"];
o)
o)// Simple XML document
o)xml: "AndrewfriendHello";
o)d: .xml.parsed[xml];
o)
o)// Navigate to specific element
o)d . (`note; `children; 0; `to; `children)
text| "Andrew"
o)
o)// Access body text
o)d . (`note; `children; 2; `body; `children)
text| "Hello"

XML with attributes:

o)// XML with element attributes
o)xml: "30";
o)d: .xml.parsed[xml];
o)
o)// Access attributes
o)d . (`person; `attrs)
id  | "123"
name| "Alice"
o)
o)// Access child element
o)d . (`person; `children; 0; `age; `children)
text| "30"

Nested elements:

o)// Nested XML structure
o)xml: "
Important
Hello World
"; o)d: .xml.parsed[xml]; o) o)// Navigate to header o)d . (`message; `children; 0; `header; `children) text| "Important" o) o)// Navigate to body o)d . (`message; `children; 1; `body; `children) text| "Hello World"

Auxiliary elements

PEG parser grammar elements:

Name Description / comments
.xml.concat[<x>] Concatenate parsed results
.xml.quotes[<x>] Parse quoted strings in attributes
.xml.spaces[<x>] Parse and skip whitespace
.xml.keyend[<x>] Parse end of element name
.xml.nident[<x>] Parse XML identifier (element/attribute name)
.xml.commnt[<x>] Parse XML comments (<!-- ... -->)
.xml.keyval[<x>] Parse attribute key-value pairs
.xml.attrbs[<x>] Parse element attributes and return `attrs dictionary
.xml.pltext[<x>] Parse plain text content and return `text field
.xml.prolog[<x>] Parse XML prolog/declaration (<?xml ... ?>)
.xml.lngtag[<x>] Parse long-form XML tags (<element>...</element>)
.xml.inltag[<x>] Parse self-closing inline tags (<element />)
.xml.contnt[<x>] Parse element content (text or nested elements)