For the complete documentation index, see llms.txt. This page is also available as Markdown.

XML

Work with XML documents using BoxLang's powerful XML type and functions

πŸ“‹ Table of Contents

πŸ™‹ What is XML in BoxLang?

XML (eXtensible Markup Language) is a markup language that defines rules for encoding documents in a format that is both human-readable and machine-readable. BoxLang provides a comprehensive XML type that implements the IStruct interface, allowing you to work with XML documents using familiar struct-like syntax while maintaining full DOM (Document Object Model) functionality.

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book id="1">
        <title>BoxLang Programming</title>
        <author>BoxLang Developer</author>
        <price>29.99</price>
    </book>
</books>

🎯 Why Use BoxLang's XML Type?

  • Struct-like Interface: Access XML elements using familiar dot notation and struct methods

  • DOM Integration: Full W3C DOM support for advanced XML manipulation

  • XPath Support: Query XML documents using powerful XPath expressions

  • Validation: Built-in DTD and XSD schema validation

  • Transformation: XSLT support for XML transformations

  • Case Sensitivity: Configurable case sensitivity for element and attribute names

  • Namespace Aware: Full XML namespace support

πŸš€ Creating XML Objects

πŸ“„ Parsing XML from Strings

πŸ†• Creating New XML Documents

πŸ—οΈ Using the XML Component

The content within the <bx:xml> tags is parsed as XML and stored in the specified variable.

🎨 XML String Interpolation (The Easy Way!)

BoxLang makes XML creation incredibly simple with string interpolation. You don't need complex builders or verbose DOM manipulation - just use variables directly in your XML strings!

πŸ”₯ Dynamic XML Generation

πŸ›‘οΈ Safe Interpolation with User Data

🎯 Conditional XML Content

🌟 Why String Interpolation is Awesome:

  • Readable: XML structure is immediately visible

  • Concise: No verbose DOM manipulation code

  • Flexible: Easy to include conditional content

  • Familiar: Uses standard BoxLang string interpolation

  • Efficient: Single parse operation vs. multiple DOM calls

πŸ“– Reading XML Data

πŸ” Accessing Elements and Attributes

πŸ“Š Working with XML Collections

πŸ”Ž XPath Queries

✏️ Modifying XML Documents

πŸ”§ Adding Elements and Attributes

πŸ“ Setting Text Content and CDATA

πŸ—‘οΈ Removing Elements

πŸ” Advanced XML Operations

πŸ”„ XML Transformations (XSLT)

πŸ›‘οΈ XML Validation

🏷️ Working with Namespaces

πŸ”§ Utility Functions

πŸ“ Element Position and Navigation

🎨 Formatting and Output

πŸ“š Common Usage Patterns

πŸ—‚οΈ Configuration Files

🌐 Web Service Responses

πŸ“Š Data Import/Export

πŸ”„ XML-to-JSON Conversion

πŸ” RSS/Atom Feed Processing

πŸ›‘οΈ Error Handling and Best Practices

🚨 Exception Management

βœ… Safe XML Access

πŸ”’ XML Security

🎯 Best Practices

βœ… Do's

  • Use XPath for complex queries instead of nested loops

  • Validate XML against schemas when data integrity is critical

  • Handle namespaces properly when working with complex XML documents

  • Cache parsed XML for frequently accessed documents

  • Use appropriate escaping for user-generated content

  • Leverage struct methods for familiar data manipulation

❌ Don'ts

  • Don't ignore validation errors - they indicate data quality issues

  • Don't modify XML during iteration - it can cause unexpected behavior

  • Don't trust external XML without validation

  • Don't forget namespace prefixes in XPath queries

  • Don't mix text content and child elements unnecessarily

  • Don't create overly deep XML structures - they're hard to maintain

🚦 Performance Tips

🎯 Optimization Guidelines

  • Use XPath wisely - complex expressions can be slow on large documents

  • Cache compiled XPath expressions for repeated queries

  • Process XML in streams for very large documents

  • Minimize DOM manipulation - batch changes when possible

  • Use appropriate validation - only validate when necessary

πŸ“Š Memory Management

πŸ”— Integration Examples

🌐 Web Services

πŸ“± Mobile API Responses

πŸ—„οΈ Database Integration

BoxLang's XML type provides a powerful, flexible way to work with XML documents that combines the familiarity of struct syntax with the full capabilities of XML DOM manipulation. Whether you're processing configuration files, web service responses, or complex data transformations, BoxLang's XML support offers the tools you need for robust XML handling.

Last updated

Was this helpful?