AppML XML Data — Reading and Writing XML Data Sources
In this tutorial, you will learn about AppML XML Data. We cover key concepts, practical examples, and best practices to help you master this topic.
AppML treats XML files as first-class data sources, reading and writing XML documents while providing the same automatic CRUD interface it offers for databases.
What You'll Learn
You will configure XML datasources in AppML, understand how XML elements map to tables and fields, and perform queries and updates on XML data.
Why It Matters
XML files are human-readable, version-control friendly, and require no database server. Configuration files, small datasets, and legacy data often come in XML format, and AppML lets you manage them through a web UI without writing parsers.
Real-World Use
DodaZIP stores compression profiles in XML files. The AppML-generated interface lets the support team edit profiles directly in the browser instead of editing XML files by hand, reducing configuration errors by 70 percent.
flowchart LR
A[XML Data File] --> B[AppML XML Parser]
B --> C[Element to Row Mapping]
C --> D[CRUD Operations]
D --> E[Modified XML]
E --> F[Write to File]
style A fill:#1e293b,color:#fff
style F fill:#0f172a,color:#fff
XML Document Structure
An XML data source is a standard XML file with repeating elements that represent data rows.
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<id>1</id>
<name>Wireless Mouse</name>
<price>29.99</price>
<category>Electronics</category>
<in_stock>true</in_stock>
</product>
<product>
<id>2</id>
<name>USB-C Hub</name>
<price>49.99</price>
<category>Electronics</category>
<in_stock>true</in_stock>
</product>
</products>
Each <product> element represents one record. Child elements map to fields. The repeating element name matches the table name in your AppML model.
Mapping XML to the Model
The AppML model for an XML datasource uses field paths that correspond to the XML element hierarchy.
<appml>
<datasource type="xml">
<connection file="./data/products.xml"/>
<table name="product" root="/products">
<field name="id" type="string" key="true"/>
<field name="name" type="string"/>
<field name="price" type="decimal"/>
<field name="category" type="string"/>
<field name="in_stock" type="boolean"/>
</table>
</datasource>
</appml>
Expected output: AppML parses the XML file, maps each product element to a row, and generates the standard CRUD interface.
The root attribute on the table element specifies the XPath to the parent container. Without it, AppML uses the document root.
Querying XML Data
AppML supports filtering and sorting on XML data sources just like database-backed models.
<view type="list" table="product">
<column field="name" header="Product Name"/>
<column field="price" header="Price" format="currency"/>
<filter field="category" default="Electronics"/>
</view>
Expected output: The list view shows only products in the Electronics category by default. The user can change the filter to see all categories.
AppML queries XML data using XPath internally. A filter on category generates:
/products/product[category='Electronics']
Expected output: The view displays matching products with prices formatted as currency.
Writing to XML
When a user creates or edits a record through the AppML interface, the XML data service modifies the XML file in place.
<!-- After adding a new product -->
<product>
<id>3</id>
<name>Mechanical Keyboard</name>
<price>89.99</price>
<category>Electronics</category>
<in_stock>false</in_stock>
</product>
Expected output: The new product element appears in the XML file in the correct position. The next page load reflects the change.
AppML locks the XML file during write operations to prevent concurrent modification conflicts.
Nested XML Structures
XML data sources support nested elements, which AppML flattens or maps to related tables.
<product>
<id>1</id>
<name>Monitor</name>
<supplier>
<name>Display Corp</name>
<contact>sales@displaycorp.com</contact>
</supplier>
</product>
Model with nested field mapping:
<field name="supplier_name" path="supplier/name" type="string"/>
<field name="supplier_contact" path="supplier/contact" type="string"/>
Expected output: Nested supplier data appears as flat fields in the form and list view.
Common Mistakes
Not setting the root path for nested XML: If your XML has a container element like
<products>, set therootattribute on the table definition to the parent XPath.Using invalid XML characters in data: Characters like &, <, and > must be escaped as &, <, and > in XML. AppML handles this when writing through the UI.
Editing the XML file directly while AppML is running: Concurrent edits may cause data loss. Always use the AppML interface or stop the server before editing the file by hand.
Forgetting to set a key field: XML files do not auto-generate keys. You must provide a unique key field and ensure new records get unique values.
Using XML for large datasets: AppML loads the entire XML file into memory. Files larger than 10 MB cause slow performance and high memory usage.
Practice Questions
- What attribute specifies the parent element for XML data?
The
rootattribute on the<table>element specifies the XPath to the container element.
- How does AppML identify records in an XML data source?
Through the key field, just like a database table. The key field value must be unique within the XML file.
- Can AppML write to XML files?
Yes. Create, update, and delete operations modify the XML file directly with file locking for concurrency.
- What happens to special characters like & in XML data?
AppML escapes them automatically when writing to XML and unescapes them when reading.
- How do you map nested XML elements to AppML fields?
Use the
pathattribute on the field definition to specify the nested element path.
Challenge
Create an XML file with product data that includes nested supplier and category elements. Write an AppML model that maps all fields including the nested ones. Add a new product through the AppML UI and verify the XML file updates correctly.
Frequently Asked Questions
Mini Project
Create an XML-based configuration manager for application settings. Store settings as key-value pairs in an XML file. Build an AppML model that lets users edit settings through a web form and saves changes back to the XML file.
What's Next
Continue to JSON Data to learn how to use JSON files and APIs as AppML data sources.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro