What Is XML? The Difference Between XML and JSON, and How to Convert
Updated: October 13, 2026 · AracMerkezi
XML (Extensible Markup Language) is a markup language that describes data with tags. Sitemap files, RSS feeds, the internal structure of Office documents, SOAP services, and many enterprise systems still use XML.
A basic example
<?xml version="1.0" encoding="UTF-8"?>
<person>
<name>Ayşe</name>
<age>30</age>
<tag>php</tag>
<tag>js</tag>
</person>
Syntax rules
- The document must have a single root element.
- Every opened tag must be closed:
<name>Ayşe</name>. - Tag names are case-sensitive:
<Name>and<name>are different. - Elements must be nested correctly;
<a><b></a></b>is invalid. - Attribute values are written in quotes:
<person id="7">. - Special characters must be escaped:
&→&,<→<,>→>.
Violating any of these rules makes the document not “well-formed” and causes the parser to report an error. The XML Formatter indents the document and shows errors of this kind.
Differences between XML and JSON
| Feature | XML | JSON |
|---|---|---|
| Structure | Tags and attributes | Objects and arrays |
| Comments | Yes (<!-- -->) | No |
| Data types | Everything is text (typed via a schema) | Text, number, boolean, null |
| Size | More verbose, larger | More compact |
| Namespaces | Supported | None |
Which should you choose?
- JSON: Generally more practical for web APIs, browser applications, and configuration files.
- XML: Suitable for document-oriented data, mixed content (tags inside text), namespaces, legacy enterprise integrations, and systems that require XML as a standard.
Converting XML to JSON
The conversion is not one-to-one, because the two formats have different models:
- Repeated elements with the same name (
<tag>twice) become an array. - Should an element that appears only once be an array or a single value? This ambiguity is the most common problem in conversion.
- Attributes are usually represented with special keys like
@id. - Mixed content (text and child elements nested together) has no natural equivalent in JSON.
The example above becomes roughly {"person": {"name": "Ayşe", "age": "30", "tag": ["php", "js"]}}. The XML to JSON Converter does this in your browser; for the reverse direction, use the JSON to XML Converter. Note that numbers come through as text after conversion.
Security note
XML parsers can be vulnerable to external entity (XXE) attacks if they are not configured. When processing XML from an untrusted source on a server, disable the parser's external entity resolution. Converter tools in the browser do not send your data to a server.
Attribute or child element?
The same information can be written in two ways: <person id="7"><name>Ayşe</name></person> or <person><id>7</id><name>Ayşe</name></person>. A general approach is to use attributes for identifiers and metadata and child elements for the actual content. Since attributes and child elements may be represented differently when converting to JSON, pay attention to the tool's settings.
Real-life examples
- RSS and Atom feeds: Blog and news feeds are XML.
- Sitemaps:
sitemap.xmlis XML and uses a namespace. - Office documents:
.docxand.xlsxfiles are actually compressed packages containing XML files. - Enterprise services: Some bank, cargo, and government services require XML over SOAP.
What is a namespace?
It prevents tags with the same name from different sources from getting mixed up: <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">. When converting documents that contain a namespace to JSON, you may see prefixes and addresses appear as extra keys; this is expected.
Debugging tips
- If the parser gives an error like “Opening and ending tag mismatch,” look for the tag that is not closed or is closed in the wrong order.
- If there is an
&in the text, make sure it has been turned into&; this is the most common mistake. - If there is an invisible character (BOM) or whitespace at the start of the document, the XML declaration may be considered invalid.
- The encoding declaration and the actual file encoding must be the same.
Limitations
The tools expect well-formed XML; they do not perform schema (XSD) validation. With very large documents, the browser's memory limit applies.
Frequently asked questions
What is the main difference between XML and JSON?
XML carries data with tags and attributes, JSON with objects and arrays; JSON is more compact, while XML offers features such as comments and namespaces.
What does well-formed XML mean?
XML that has a single root element, with tags correctly opened and closed and special characters escaped.
Can XML be converted to JSON one-to-one?
Not always; repeated elements, attributes, and mixed content may not have a natural JSON equivalent.
Tools mentioned in this guide
More guides
- What Are JSON, Base64 and URL Encoding? Encoding vs Encryption vs Hashing – What do JSON, Base64, URL encoding, HTML entities, SHA and JWT do? Encoding vs encryption vs hashing, with examples that were run and verified.
- What Is JSON? Structure, Formatting, Validation and Finding Errors – What is JSON and how do you read and validate it? A nested example, a valid/invalid comparison table, number and date traps and the formatting difference.
- What Is Semver? MAJOR.MINOR.PATCH Version Numbers and ^ ~ Ranges – What is semantic versioning (semver)? MAJOR.MINOR.PATCH bump rules, pre-release ordering, ^ and ~ ranges and which version gets installed, with examples.
- What Is YAML? Indentation Rules, Differences from JSON, and Common Mistakes – What is YAML, and how does indentation work? Differences between YAML and JSON, common mistakes (tabs, colons, yes/no), and converting YAML to JSON.
- What Is curl? Basic Flags and Converting a curl Command to fetch – What is curl and what do the -X, -H and -d flags do? A POST request example, the JavaScript fetch equivalent of a curl command, and debugging tips.
This article is for general information only and is not legal, financial or medical advice. We are not an official institution.