免费XML转JSON转换器

即时将XML转换为JSON。支持属性、嵌套元素和文本节点。完全在您的浏览器中运行。

您的数据从未离开设备
将XML文件拖放到此处 或点击浏览 (最大5 MB)

什么是XML?

XML(可扩展标记语言)是一种用于存储和传输数据的格式。它使用自定义标签来描述数据结构,使其人类可读且广泛受支持。与JSON不同,XML可以在元素上包含属性。

为什么要将XML转换为JSON?

常见问题

XML属性是如何处理的?

XML属性被转换为带有@前缀的JSON属性(例如@id、@href)。这在JSON结构中保留了属性信息。

命名空间怎么处理?

命名空间会在输出中保留。前缀会包含在元素名称中(例如“ns:element”)。

可以将JSON反转为XML吗?

此工具将XML转换为JSON。反向转换请使用我们的JSON转XML转换器。

Two formats, three decades apart

XML 1.0 became a W3C Recommendation on 10 February 1998. The original editors were Tim Bray (Textuality / Netscape), Jean Paoli (Microsoft) and C. M. Sperberg-McQueen (University of Illinois at Chicago); the Working Group was chaired by Jon Bosak of Sun Microsystems. XML's lineage runs back to SGML (ISO 8879:1986), the document-markup standard that itself grew from IBM's GML in the 1970s. SGML was powerful but daunting; XML's design brief was "design a 'lite' version of SGML", trimming optional features to a profile that could be implemented in a few thousand lines of code while preserving Unicode, namespaces and attribute-rich documents.

JSON (JavaScript Object Notation) was discovered, not designed. Douglas Crockford and Chip Morningstar sent the first JSON message in April 2001 at State Software, deriving the format from JavaScript's object-literal syntax. Crockford registered json.org in 2002 and put up a one-page spec. The format was first formalised as RFC 4627 in July 2006, re-issued as RFC 7159 in 2014, and stabilised as RFC 8259 / STD 90 in December 2017, the same year ECMA-International published ECMA-404. JSON's design intent (per Crockford's retrospective): minimal, language-independent, easy to generate and parse, no version number ("there is no version number"), and deliberately no comments ("I removed comments because I saw people were using them to hold parsing directives").

JSON took over web APIs in the late 2000s as REST replaced SOAP and as XMLHttpRequest gave way to JSON-over-fetch. By 2015 JSON was the default response format of nearly every public API; XML survived in long-tail enterprise SOA, document formats (Office Open XML, OpenDocument), and standards-bound niches.

Where XML and JSON differ

XMLJSON
AttributesFirst-class (<item id="5"/>)No attributes, everything is a key/value pair
NamespacesNative (xmlns:prefix="uri")Flat names; prefix collisions are the app's problem
Comments<!-- … -->None per spec
Mixed contentNative (<p>text <b>and</b> markup</p>)Awkward, needs arrays or stringification
SchemaDTD, XSD 1.0 (2001) / 1.1 (2012), RELAX NG, SchematronJSON Schema (community spec, draft 2020-12)
OrderElement order is significant per specObject key order not guaranteed (most parsers preserve insertion order in practice); arrays are ordered
NumbersStrings until typed by schemaIEEE 754 double; no integer/float distinction at spec level
VerbosityHigh (every value wrapped in tagsLow) punctuation only

Why XML→JSON is fundamentally lossy

There is no W3C-blessed canonical mapping from XML to JSON. Every converter has to invent answers to questions XML allows you to express but JSON has no native vocabulary for. The recurring decisions:

When you'd reach for this

Modern alternatives

More questions

Why does the same XML produce a different JSON shape on different tools?

Because there is no canonical XML→JSON mapping. Each converter picks a convention (BadgerFish, Parker, JsonML, GData-style) and the resulting JSON shapes differ in how attributes are marked, how mixed content is preserved, and how repeated children are arrayed. Round-tripping XML through different converters produces different JSON; round-tripping JSON back to XML through different converters can change attribute placement and even element ordering. For interoperability, document the convention you're using and stick to it.

What about CDATA, comments and processing instructions?

CDATA sections (<![CDATA[…]]>) are flattened to plain string content, the wrapper is gone, but the text inside is preserved verbatim. XML comments (<!-- … -->) and processing instructions (<?xml-stylesheet …?>) are dropped, since JSON has no syntax for either. If you need round-trip fidelity that preserves comments, an XML-only round trip is the right approach.

Will my XML schema (XSD) survive the conversion?

No. XSD describes XML structure and types; JSON Schema is the analogous (separate) standard for JSON. Some advanced tooling can translate XSD to JSON Schema, but it's a lossy operation, XSD has features (mixed content, substitution groups, derived types) that have no clean JSON Schema equivalent. For document validation that matters, run XSD against the original XML, not against the JSON conversion.

Why did JSON win for web APIs?

A few reasons. JSON maps directly to JavaScript objects, so browser-side parsing is one JSON.parse() call away. The format is much smaller, no closing tags, no namespace declarations, no schema baggage. REST replaced SOAP for most public APIs in the late 2000s, and JSON was the natural payload for REST. XML's strengths (strict schemas, namespaces, mixed content) matter for documents and legacy enterprise systems but rarely for "give me the user object." The web optimised for the simpler case.

Does anything get sent to a server?

No. The XML is parsed by the browser's native DOMParser, then walked recursively in JavaScript to build the JSON output. Pasted XML never leaves the page. The tool works offline once it's loaded, which matters when the source XML contains internal API endpoints, customer data, or proprietary schemas you'd prefer not to upload anywhere.

相关工具