XML Serialization and Parsing module based on Python's ElementTree.
npx @tessl/cli install tessl/npm-elementtree@0.1.0ElementTree 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.
npm install elementtreeconst et = require('elementtree');
const Element = et.Element;
const SubElement = et.SubElement;
const ElementTree = et.ElementTree;
const XML = et.XML;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);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"ElementTree is built around several key components:
Element class representing XML nodes with attributes, text content, and childrenElementTree class providing document-level operations and XPath queriesTreeBuilder and configurable parsersCore functionality for creating and manipulating XML elements, including factory functions, attribute management, and tree operations.
function Element(tag, attrib);
function SubElement(parent, tag, attrib);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);
}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);Convert XML documents and elements to string format with comprehensive formatting and namespace options.
function tostring(element, options);
// ElementTree.write() method
write(options);Support for XML comments, CDATA sections, and processing instructions.
function Comment(text);
function CData(text);
function ProcessingInstruction(target, text);XML namespace handling including qualified names and namespace registration.
function QName(text_or_uri, tag);
function register_namespace(prefix, uri);// 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
}