🔄 XML to JSON

📐 工作原理与说明
DOMParser 解析,出现 parsererror 节点即判失败。递归规则:节点有属性时以 @属性名 为键写入;无子元素时取 textContent 去空白,有属性则存为 #text,全空返回空串;有子元素时按 tagName 递归,同名第二次出现转为数组、第三次起 push。输出 JSON.stringify(result, null, indent)
本开发工具在前端本地完成解析、转换与处理,代码不离开浏览器,遵循对应语法与格式规范。 工具名称:XML 转 JSON - 免费开发工具,在线XML 转 JSON,免费使用。

Convert XML documents into JSON with attributes and text nodes.

等待转换...

📚 深度解析:XML 转 JSON

💡 常见使用场景

  • 把 XML 响应转 JSON 供前端使用。
  • 把只提供 XML 的旧接口响应转成 JSON,供前端或现代服务消费,省去解析 XML 的依赖。
转换
`<name>Tom</name>` → `{"name":"Tom"}`。

❓ 常见问题(FAQ)

属性怎么转?
常加 @ 前缀,如 `<a id="1">` → `{"a":{"@id":"1"}}`。
为什么单个元素有时是对象有时是数组?
这是 XML 转 JSON 的经典问题:XML 无法表达「数组」概念,转换器只能按出现次数推断,导致同一字段在只有一项时是对象、多项时是数组,消费端代码容易出错。解决办法是在转换时显式声明哪些路径强制为数组,或在消费端统一做归一化处理。

Formula & Standard References

Item Formula / Note Source / Standard
Elements and attributes elements become keys; attributes often prefixed with @ W3C XML 1.0
Repeated elements same-name children collapse into an array in order W3C XML 1.0
Text nodes text-only elements map to strings; mixed content uses #text W3C XML 1.0
⚠️ 使用说明与注意事项