XML转JSON
解析 XML 文档为等价的 JSON 对象。
关于「XML转JSON」
它属于「JSON 工具」分类:JSON 的格式化、结构变换与各种数据格式互转。
- 使用浏览器内置的
DOMParser解析,遵循标准 XML 规范。 - 元素的属性会转成带前缀的键,例如
id="1"→"@id": "1"。 - 同名的兄弟标签会自动合并为数组,单个则保持对象形式。
- 只有文本内容、没有属性和子元素的节点,会直接简化为字符串。
- 解析失败时会提示具体的错误原因,便于定位标签未闭合等问题。
完全免费、无需注册;所有处理都在浏览器本地完成,文件不会上传到任何服务器。
相关关键词:XML转JSON、JSON 工具、xml、解析、在线工具、免费
如何使用「XML转JSON」
- 打开工具:打开「XML转JSON」页面,无需注册、无需安装任何软件。
- 准备输入:粘贴 XML 文档或上传文件,解析为等价的 JSON 对象。
- 填入内容:把内容粘贴进输入框,或把文件拖入页面;也可以点击「示例」先填入样例数据试一下。
- 设置选项并处理:按需调整页面上的选项,然后点击处理 / 转换 / 生成按钮。
- 获取结果:处理完全在浏览器本地完成,结果可直接复制或下载,文件不会上传到任何服务器。
常见问题
- 属性前缀可以改吗?
- 可以,把前缀改成
$、_或留空都行。留空时要注意属性名可能与子标签名冲突而互相覆盖。 - 为什么有的节点是字符串,有的是对象?
- 为了让结果更简洁:如果一个节点既没有属性也没有子元素,就直接用它的文本值表示;只要存在属性或子元素,就必须用对象来承载。
- 同名标签只有一个时也是数组吗?
- 不是。只出现一次时保持对象或字符串形式。如果你的代码需要稳定的数组类型,记得在读取时做一次兼容判断。
- 提示解析失败怎么办?
- 通常是 XML 本身不合法,比如标签没有闭合、存在多个根节点,或含有未转义的
&符号。请按提示信息检查源文档。
XML to JSON
Parse an XML document into an equivalent JSON object.
About XML to JSON
Belongs to “JSON Tools”: Format, transform and convert JSON to and from various data formats.
- Parsed with the browser’s built-in
DOMParser, following standard XML rules. - Element attributes become prefixed keys, e.g.
id="1"→"@id": "1". - Sibling tags with the same name auto-merge into an array; a single one stays an object.
- A node with only text and no attributes or children collapses to a plain string.
- On failure it reports the specific reason, helping locate unclosed tags, etc.
Free, no sign-up; everything runs locally in your browser and files are never uploaded.
Keywords: XML to JSON, JSON Tools, xml, parse, online tool, free
How to use XML to JSON
- Open the tool:Open the “XML to JSON” page — no sign-up and nothing to install.
- Prepare the input:Paste an XML document or upload a file to parse it into an equivalent JSON object.
- Provide the input:Paste your content into the input box or drop a file onto the page; you can also click “Sample” to try example data first.
- Adjust options and run:Adjust the options if needed, then click the process / convert / generate button.
- Get the result:Everything runs locally in your browser; copy or download the result — your files are never uploaded.
FAQ
- Can the attribute prefix change?
- Yes — change it to
$,_or leave it empty. If empty, an attribute name may collide with and overwrite a child tag. - Why are some nodes strings and others objects?
- To keep results compact: a node with neither attributes nor children becomes its text value; once attributes or children exist, an object is required.
- Is a single same-named tag also an array?
- No. A single occurrence stays an object or string. If your code needs a stable array type, add a guard when reading.
- Parse failed — what now?
- Usually the XML is invalid — unclosed tags, multiple roots, or an unescaped
&. Check the source per the message.