100% Free Forever — No signup, no paywalls, no limits.
DevKit
XML 4 min read January 29, 2026

XML Formatting in 2026: Tools, Techniques, and Best Practices

How to format and beautify XML documents with proper indentation, namespace handling, and structure validation — for modern and legacy systems.

DK

DevKit Team

Engineering

Share:

XML remains essential in enterprise environments, SOAP APIs, and configuration formats. Properly formatted XML with consistent indentation makes complex documents scannable and helps spot structural errors. In 2026, browser-based XML formatters handle namespace declarations, CDATA sections, and mixed content correctly.

Formatting Best Practices

Use 2-space indentation for most XML documents. Keep namespace declarations at the root element. Preserve CDATA sections and comment positions during formatting. For large XML files, use streaming formatters that don't load the entire document into memory. Validate against an XSD schema after formatting to ensure the structure wasn't altered.

Why This Matters in 2026

XML is far from dead in 2026. Enterprise systems, SOAP APIs, SVG graphics, Office document formats, and configuration files all rely on XML. While JSON won the web API war, XML remains essential for document-centric data, enterprise integrations, and systems that require schema validation with XSD. Understanding XML formatting, namespace handling, and conversion between JSON and XML is a valuable skill for any developer working with enterprise or legacy systems.

Key Takeaways

  • XML remains essential for SOAP APIs, enterprise systems, and document formats
  • Use 2-space indentation for XML formatting — never tabs
  • Validate XML against XSD schemas after any transformation
  • Handle namespaces carefully — declare at the root element level
  • Preserve CDATA sections and comments during formatting operations
  • Use streaming parsers for large XML files to avoid memory issues

Common Mistakes to Avoid

  • Using tabs instead of spaces for XML indentation — causes parsing issues
  • Not declaring namespaces at the root element, causing scope confusion
  • Losing CDATA sections during formatting or transformation
  • Not validating against XSD after converting from JSON
  • Using regex to parse XML — use a proper XML parser instead
  • Ignoring character encoding declarations, causing mojibake in output

Warning

Never parse XML with regex. XML supports nested structures, namespaces, CDATA sections, and character encoding that regex cannot handle correctly. Always use a dedicated XML parser like fast-xml-parser or DOMParser.

Best Practices

  • Use 2-space indentation consistently for all XML documents
  • Keep namespace declarations at the root element level
  • Preserve CDATA sections and comment positions during formatting
  • Validate XML output against the expected XSD schema before sending
  • Use streaming parsers (SAX, StAX) for files larger than 50MB
  • Always specify character encoding in the XML declaration

Tip

When converting JSON to XML, use a convention for attributes — prefix attribute keys with "@" or use a wrapper element. Without a convention, the conversion is ambiguous and error-prone.

Quick Reference

Here is a quick reference for XML formatting and parsing in JavaScript using the DOMParser API:

javascript
// Parse XML in the browser
const parser = new DOMParser();
const doc = parser.parseFromString(xmlString, "text/xml");
const error = doc.querySelector("parsererror");
if (error) console.error("Invalid XML:", error.textContent);

// Format XML with proper indentation
function formatXML(xml) {
  const doc = new DOMParser().parseFromString(xml, "text/xml");
  return new XMLSerializer().serializeToString(doc);
}

// Select elements with namespaces
const elements = doc.getElementsByTagNameNS(
  "http://example.com/ns", "item"
);

Real-World Example

A common enterprise scenario: receiving a SOAP response from a legacy API and extracting the relevant data. Here is how to parse and navigate the XML response safely:

javascript
async function callSoapApi(url, body) {
  const res = await fetch(url, {
    method: "POST",
    headers: { "Content-Type": "text/xml; charset=utf-8" },
    body,
  });
  const xmlText = await res.text();
  const doc = new DOMParser().parseFromString(xmlText, "text/xml");

  // Check for SOAP fault
  const fault = doc.querySelector("Fault");
  if (fault) throw new Error("SOAP fault: " + fault.textContent);

  // Extract result
  const result = doc.querySelector("Result");
  return result?.textContent;
}

Tools and Resources

  • DevKit XML Formatter — browser-based XML beautifier with namespace support
  • DevKit JSON to XML — convert JSON payloads to well-formed XML
  • fast-xml-parser — fast Node.js XML parser with bidirectional conversion
  • xmllint — command-line XML validator and formatter
  • XSD schema validators — validate XML structure against defined schemas

"XML is like violence — if it does not solve your problem, you are not using enough of it. (But seriously, use JSON for APIs.)"

XML remains essential in enterprise environments, SOAP APIs, and document-centric formats. Format with 2-space indentation, validate against XSD schemas, handle namespaces at the root level, and always use a proper XML parser — never regex. When converting between JSON and XML, use clear conventions for attributes and nested structures.

Advertisement
32 tools ready to use

Ready to boost your workflow?

No accounts. No uploads. No limits. Just open a tool and start working.

Browse All Tools
Free forever
No signup
100% private