or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

elements.mdindex.mdnamespaces.mdparsing.mdserialization.mdspecial-elements.mdxpath.md
tile.json

index.mddocs/

ElementTree

ElementTree is a Node.js XML parsing and serialization library based on Python's ElementTree v1.3 module. It provides a comprehensive API for creating, parsing, manipulating, and serializing XML documents with XPath support and namespace handling.

Package Information

  • Package Name: elementtree
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install elementtree

Core Imports

const et = require('elementtree');
const Element = et.Element;
const SubElement = et.SubElement;
const ElementTree = et.ElementTree;
const XML = et.XML;

Basic Usage

Creating XML Documents

const et = require('elementtree');
const Element = et.Element;
const SubElement = et.SubElement;
const ElementTree = et.ElementTree;

// Create root element
const root = Element('entry');
root.set('xmlns', 'http://www.w3.org/2005/Atom');

// Add child elements
const tenantId = SubElement(root, 'TenantId');
tenantId.text = '12345';

const serviceName = SubElement(root, 'ServiceName');
serviceName.text = 'MaaS';

// Serialize to XML
const etree = new ElementTree(root);
const xml = etree.write({'xml_declaration': false});
console.log(xml);

Parsing XML Documents

const et = require('elementtree');

// Parse XML string
const etree = et.parse('<root><item id="1">Hello</item></root>');

// Use XPath to find elements
console.log(etree.findtext('item')); // "Hello"
console.log(etree.find('item').get('id')); // "1"

Architecture

ElementTree is built around several key components:

  • Element System: Core Element class representing XML nodes with attributes, text content, and children
  • Document Model: ElementTree class providing document-level operations and XPath queries
  • Parser Infrastructure: SAX-based parsing using TreeBuilder and configurable parsers
  • XPath Engine: Custom XPath implementation supporting element selection and filtering
  • Serialization Engine: XML serialization with namespace support, formatting options, and special element handling

Capabilities

Element Creation and Manipulation

Core functionality for creating and manipulating XML elements, including factory functions, attribute management, and tree operations.

function Element(tag, attrib);
function SubElement(parent, tag, attrib);

Element Operations

XML Parsing and Document Operations

Parse XML documents and perform document-level operations including XPath queries and tree navigation.

function parse(source, parser);
function XML(data);

class ElementTree {
  constructor(element);
  parse(source, parser);
  getroot();
  find(path);
  findall(path);
  findtext(path, defvalue);
  write(options);
}

XML Parsing

XPath Operations

XPath expression support for finding and selecting elements within XML documents.

// Available on both Element and ElementTree instances
find(path);
findall(path);
findtext(path, defvalue);

XPath Operations

XML Serialization

Convert XML documents and elements to string format with comprehensive formatting and namespace options.

function tostring(element, options);

// ElementTree.write() method
write(options);

XML Serialization

Special XML Elements

Support for XML comments, CDATA sections, and processing instructions.

function Comment(text);
function CData(text);
function ProcessingInstruction(target, text);

Special Elements

Namespace Management

XML namespace handling including qualified names and namespace registration.

function QName(text_or_uri, tag);
function register_namespace(prefix, uri);

Namespace Management

Types

// Element instance properties and methods
interface Element {
  tag: string;
  attrib: object;
  text: string | null;
  tail: string | null;
  
  // Element manipulation methods
  len(): number;
  append(element): void;
  remove(element): void;
  clear(): void;
  
  // Attribute methods
  get(key, defvalue): any;
  set(key, value): void;
  keys(): string[];
  items(): Array<[string, any]>;
  
  // XPath methods
  find(path): Element | null;
  findall(path): Element[];
  findtext(path, defvalue): string | null;
  
  // Iteration methods
  iter(tag, callback): void;
  itertext(callback): void;
}

// Write options for serialization
interface WriteOptions {
  encoding?: string;           // default: 'utf-8'
  xml_declaration?: boolean;   // default: true
  default_namespace?: string;  // default: null
  method?: string;            // 'xml' or 'text', default: 'xml'
  indent?: number;            // spaces for pretty printing
}