Core functionality for creating and manipulating XML elements, including factory functions, attribute management, and tree operations.
Creates a new XML element with optional attributes.
/**
* Create a new XML element
* @param {string} tag - Element tag name
* @param {object} attrib - Optional attributes object
* @returns {Element} New Element instance
*/
function Element(tag, attrib);Usage Examples:
const et = require('elementtree');
// Create element without attributes
const root = et.Element('root');
// Create element with attributes
const item = et.Element('item', {id: '123', type: 'data'});Creates a new child element and appends it to the given parent element.
/**
* Create a child element and append to parent
* @param {Element} parent - Parent element
* @param {string} tag - Child element tag name
* @param {object} attrib - Optional attributes object
* @returns {Element} New child Element instance
*/
function SubElement(parent, tag, attrib);Usage Examples:
const et = require('elementtree');
const root = et.Element('root');
const child = et.SubElement(root, 'child', {name: 'test'});
child.text = 'Hello World';Core properties available on every Element instance.
interface ElementProperties {
/** Element tag name */
tag: string;
/** Element attributes as object */
attrib: object;
/** Element text content */
text: string | null;
/** Text content after element (used in serialization) */
tail: string | null;
}Methods for managing child elements within an element.
/**
* Get number of child elements
* @returns {number} Number of child elements
*/
len(): number;
/**
* Append child element
* @param {Element} element - Element to append
*/
append(element): void;
/**
* Set element at specific index (overwrites existing element)
* @param {number} index - Index position
* @param {Element} element - Element to set at index
* @note This method overwrites the element at the given index, it does not insert
*/
insert(index, element): void;
/**
* Extend element with multiple child elements
* @param {Element[]} elements - Array of elements to append
*/
extend(elements): void;
/**
* Remove specific child element
* @param {Element} element - Element to remove
*/
remove(element): void;
/**
* Get all child elements
* @returns {Element[]} Array of child elements
*/
getchildren(): Element[];
/**
* Clear all attributes, children, and text content
*/
clear(): void;Usage Examples:
const et = require('elementtree');
const root = et.Element('root');
const child1 = et.Element('child1');
const child2 = et.Element('child2');
// Add children individually
root.append(child1);
root.append(child2);
console.log(root.len()); // 2
// Or add multiple children at once using extend
const root2 = et.Element('root2');
const children = [et.Element('child3'), et.Element('child4'), et.Element('child5')];
root2.extend(children);
console.log(root2.len()); // 3
// Remove child
root.remove(child1);
console.log(root.len()); // 1
// Clear all content
root.clear();
console.log(root.len()); // 0Methods providing array-like access to child elements.
/**
* Get child element at index
* @param {number} index - Child element index
* @returns {Element} Child element at index
*/
getItem(index): Element;
/**
* Set child element at index
* @param {number} index - Child element index
* @param {Element} element - Element to set
*/
setItem(index, element): void;
/**
* Delete child element at index
* @param {number} index - Child element index
*/
delItem(index): void;
/**
* Get slice of child elements
* @param {number} start - Start index
* @param {number} stop - Stop index
* @returns {Element[]} Array slice of child elements
*/
getSlice(start, stop): Element[];
/**
* Set slice of child elements (replaces existing elements in range)
* @param {number} start - Start index
* @param {number} stop - Stop index
* @param {Element[]} elements - Elements to replace the range with
* @note Only replaces existing elements, does not expand the children array
*/
setSlice(start, stop, elements): void;
/**
* Delete slice of child elements
* @param {number} start - Start index
* @param {number} stop - Stop index
*/
delSlice(start, stop): void;Methods for working with element attributes.
/**
* Get attribute value
* @param {string} key - Attribute name
* @param {any} defvalue - Default value if attribute not found
* @returns {any} Attribute value or default value
*/
get(key, defvalue): any;
/**
* Set attribute value
* @param {string} key - Attribute name
* @param {any} value - Attribute value
*/
set(key, value): void;
/**
* Get array of attribute names
* @returns {string[]} Array of attribute keys
*/
keys(): string[];
/**
* Get array of [key, value] attribute pairs
* @returns {Array<[string, any]>} Array of attribute pairs
*/
items(): Array<[string, any]>;Usage Examples:
const et = require('elementtree');
const element = et.Element('item');
// Set attributes
element.set('id', '123');
element.set('type', 'data');
// Get attributes
console.log(element.get('id')); // '123'
console.log(element.get('missing', 'default')); // 'default'
// List attributes
console.log(element.keys()); // ['id', 'type']
console.log(element.items()); // [['id', '123'], ['type', 'data']]Each element can create new elements using the same factory as its parent.
/**
* Create new element using same factory as current element
* @param {string} tag - Element tag name
* @param {object} attrib - Optional attributes object
* @returns {Element} New Element instance
*/
makeelement(tag, attrib): Element;Methods for iterating over element descendants and text content.
/**
* Iterate over element and its descendants
* @param {string|null} tag - Tag name to filter by, or null for all elements
* @param {function} callback - Callback function receiving each element
*/
iter(tag, callback): void;
/**
* Iterate over all text content in element and descendants
* @param {function} callback - Callback function receiving each text segment
*/
itertext(callback): void;Usage Examples:
const et = require('elementtree');
const root = et.Element('root');
const child1 = et.SubElement(root, 'item');
child1.text = 'First';
const child2 = et.SubElement(root, 'item');
child2.text = 'Second';
// Iterate over all elements
root.iter(null, function(element) {
console.log(element.tag);
});
// Output: root, item, item
// Iterate over specific tag
root.iter('item', function(element) {
console.log(element.text);
});
// Output: First, Second
// Iterate over text content
root.itertext(function(text) {
console.log(text);
});
// Output: First, SecondElement string representation for debugging.
/**
* Get string representation of element
* @returns {string} String representation
*/
toString(): string;