Generate random XML data with customizable depth and element count
Nesting level (1-10)
Max elements per level (1-20)
Random XML generator creates valid XML documents with random structure and data. Generate test XML for parser testing, API mocking, data import testing, and schema validation with customizable elements, attributes, and nesting.
Define schema or structure template, then recursively generate elements with random content. Create opening tags, generate attributes, produce child elements or text content, close tags. Ensure well-formed XML with proper escaping.
XML requirements: single root element, properly nested tags, escaped special characters (& < > " '), optional declaration (<?xml version="1.0"?>). Attributes in quotes, case-sensitive tags. Valid: element names start with letter/underscore.
Generate varied XML to test parsing robustness.
Create XML responses for SOAP/REST testing.
Generate XML files for import functionality.
Test XSD validators with random valid XML.
// Simple random XML generator
function generateRandomXML(options = {}) {
const {
rootName = 'root',
maxDepth = 3,
maxChildren = 5,
includeAttributes = true
} = options;
function escapeXML(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function randomString(length = 10) {
const chars = 'abcdefghijklmnopqrstuvwxyz';
return Array(length).fill(0)
.map(() => chars[Math.floor(Math.random() * chars.length)])
.join('');
}
function generateElement(name, depth) {
let xml = `<${name}`;
// Random attributes
if (includeAttributes && Math.random() > 0.5) {
const attrCount = Math.floor(Math.random() * 3) + 1;
for (let i = 0; i < attrCount; i++) {
xml += ` ${randomString(5)}="${escapeXML(randomString(8))}"`;
}
}
if (depth >= maxDepth || Math.random() > 0.7) {
// Leaf node with text content
xml += `>${escapeXML(randomString(20))}</${name}>`;
} else {
// Branch node with children
xml += '>\n';
const childCount = Math.floor(Math.random() * maxChildren) + 1;
for (let i = 0; i < childCount; i++) {
const indent = ' '.repeat(depth + 1);
xml += indent + generateElement(randomString(6), depth + 1) + '\n';
}
xml += ' '.repeat(depth) + `</${name}>`;
}
return xml;
}
const declaration = '<?xml version="1.0" encoding="UTF-8"?>\n';
return declaration + generateElement(rootName, 0);
}
// Structured XML generation
function generateOrderXML() {
const order = {
id: Math.floor(Math.random() * 100000),
date: new Date().toISOString(),
customer: {
name: 'John Doe',
email: 'john@example.com'
},
items: Array(Math.floor(Math.random() * 5) + 1).fill(0).map((_, i) => ({
sku: 'SKU' + Math.floor(Math.random() * 10000),
quantity: Math.floor(Math.random() * 10) + 1,
price: (Math.random() * 100).toFixed(2)
}))
};
return `<?xml version="1.0"?>
<order id="${order.id}" date="${order.date}">
<customer>
<name>${order.customer.name}</name>
<email>${order.customer.email}</email>
</customer>
<items>
${order.items.map(item => ` <item sku="${item.sku}">
<quantity>${item.quantity}</quantity>
<price>${item.price}</price>
</item>`).join('\n')}
</items>
</order>`;
}Generates random or structured XML with proper escaping and nesting.
Use random XML generator for testing XML parsers, creating mock API responses, testing data imports, or generating sample data for XML-based systems.
Escape special characters, single root element, properly close all tags, valid element names (no spaces, start with letter). Use XML library for generation to handle escaping.