XMLからJSONへの変換は、現代のソフトウェア開発で最も一般的なデータ変換タスクの一つです。設定ファイル、RSSフィード、SOAP APIレスポンスを処理する場合でも、効率的なXMLからJSON変換ツールは手作業の時間を大幅に節約します。この包括的なガイドでは、パースメカニズムからJavaScript、Python、Java、Bashのコード例まですべてをカバーします。
無料オンラインXMLからJSON変換ツールをお試しください。
XMLからJSON変換とは?
XMLとJSONは最も広く使われている2つのデータ交換形式です。XMLは数十年にわたりエンタープライズコンピューティングを支配してきました。JSONはWeb開発者に好まれる軽量な代替手段として登場しました。
XMLからJSON変換器は整形式のXMLドキュメントを受け取り、要素、属性、名前空間、CDATAセクションを処理しながら同等のJSON表現に変換します。
逆の操作であるJSONからXML変換は、既存のXMLシステムとの統合時に同様に重要です。
XML vs JSON:詳細比較
変換技術に入る前に、XMLとJSONの構造的違いを理解することが重要です:
| 特性 | XML | JSON |
|---|---|---|
| 構文 | タグベースのマークアップ | キーバリューペア |
| 属性 | ネイティブサポート | 属性の概念なし |
| 名前空間 | 完全サポート | ネイティブサポートなし |
| サイズ | より大きい | 30-50%小さい |
XMLからJSON変換の仕組み
変換プロセスにはいくつかの重要なステップがあります:
- XMLドキュメントのパース:DOMツリーの構築またはSAXイベント。
- 要素をオブジェクトにマッピング:各要素がJSONプロパティになります。
- 属性の処理:
@などのプレフィックスを使用。 - テキストノードの処理:文字列値への直接マッピング。
- 繰り返し要素を配列に変換。
- 名前空間の処理。
いくつかの確立された規約がXMLからJSONへのマッピングを定義します:
- Badgerfish規約:すべてのXML情報を保持。
- Parker規約:最もコンパクトなJSON。
- GData規約:Googleが使用する中間的アプローチ。
XMLからJSONコード例
JavaScript:XMLからJSON
JavaScriptではfast-xml-parserとxml2jsが堅牢な変換を提供します:
// ===== Using fast-xml-parser (recommended) =====
// npm install fast-xml-parser
import { XMLParser, XMLBuilder } from 'fast-xml-parser';
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1" category="fiction">
<title lang="en">The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<price currency="USD">10.99</price>
<year>1925</year>
</book>
<book id="2" category="non-fiction">
<title lang="en">Sapiens</title>
<author>Yuval Noah Harari</author>
<price currency="USD">14.99</price>
<year>2011</year>
</book>
</bookstore>`;
// Configure parser with attribute handling
const parser = new XMLParser({
ignoreAttributes: false, // preserve attributes
attributeNamePrefix: '@_', // prefix for attributes
textNodeName: '#text', // key for text content
isArray: (name, jpath) => { // force arrays for known collections
return ['bookstore.book'].includes(jpath);
},
});
const json = parser.parse(xml);
console.log(JSON.stringify(json, null, 2));
// Output:
// {
// "bookstore": {
// "book": [
// {
// "@_id": "1",
// "@_category": "fiction",
// "title": { "@_lang": "en", "#text": "The Great Gatsby" },
// "author": "F. Scott Fitzgerald",
// "price": { "@_currency": "USD", "#text": 10.99 },
// "year": 1925
// },
// ...
// ]
// }
// }
// ===== Using DOMParser (browser built-in) =====
function xmlToJson(xmlString) {
const parser = new DOMParser();
const doc = parser.parseFromString(xmlString, 'text/xml');
function nodeToJson(node) {
const obj = {};
// Handle attributes
if (node.attributes && node.attributes.length > 0) {
for (let i = 0; i < node.attributes.length; i++) {
const attr = node.attributes[i];
obj['@' + attr.nodeName] = attr.nodeValue;
}
}
// Handle child nodes
if (node.childNodes && node.childNodes.length > 0) {
for (let i = 0; i < node.childNodes.length; i++) {
const child = node.childNodes[i];
if (child.nodeType === 1) { // Element node
const childObj = nodeToJson(child);
if (obj[child.nodeName]) {
// Convert to array if duplicate element names
if (!Array.isArray(obj[child.nodeName])) {
obj[child.nodeName] = [obj[child.nodeName]];
}
obj[child.nodeName].push(childObj);
} else {
obj[child.nodeName] = childObj;
}
} else if (child.nodeType === 3) { // Text node
const text = child.nodeValue.trim();
if (text) {
if (Object.keys(obj).length === 0) return text;
obj['#text'] = text;
}
} else if (child.nodeType === 4) { // CDATA section
obj['#cdata'] = child.nodeValue;
}
}
}
return obj;
}
const root = doc.documentElement;
const result = {};
result[root.nodeName] = nodeToJson(root);
return result;
}
// ===== Using xml2js (Node.js) =====
// npm install xml2js
import { parseString } from 'xml2js';
parseString(xml, {
explicitArray: false,
mergeAttrs: true,
trim: true,
}, (err, result) => {
if (err) throw err;
console.log(JSON.stringify(result, null, 2));
});
// ===== Streaming large XML with sax (Node.js) =====
// npm install sax
import sax from 'sax';
const saxParser = sax.createStream(true, { trim: true });
const stack = [];
let current = {};
saxParser.on('opentag', (node) => {
const obj = {};
if (node.attributes) {
for (const [key, value] of Object.entries(node.attributes)) {
obj['@' + key] = value;
}
}
stack.push(current);
current[node.name] = obj;
current = obj;
});
saxParser.on('text', (text) => {
if (text.trim()) current['#text'] = text.trim();
});
saxParser.on('closetag', () => {
current = stack.pop();
});Python:XMLからJSON
Pythonではxmltodictが最も人気の選択肢です:
# ===== Using xmltodict (most popular) =====
# pip install xmltodict
import xmltodict
import json
xml_string = """<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<product id="101" category="electronics">
<name>Wireless Mouse</name>
<price currency="USD">29.99</price>
<specs>
<weight unit="g">85</weight>
<battery>AA</battery>
<connectivity>Bluetooth 5.0</connectivity>
</specs>
<tags>
<tag>wireless</tag>
<tag>mouse</tag>
<tag>bluetooth</tag>
</tags>
</product>
</catalog>
"""
# Convert XML to Python dict (then to JSON)
data = xmltodict.parse(xml_string)
json_output = json.dumps(data, indent=2, ensure_ascii=False)
print(json_output)
# Output:
# {
# "catalog": {
# "product": {
# "@id": "101",
# "@category": "electronics",
# "name": "Wireless Mouse",
# "price": { "@currency": "USD", "#text": "29.99" },
# "specs": {
# "weight": { "@unit": "g", "#text": "85" },
# "battery": "AA",
# "connectivity": "Bluetooth 5.0"
# },
# "tags": { "tag": ["wireless", "mouse", "bluetooth"] }
# }
# }
# }
# Force specific elements to always be lists
data = xmltodict.parse(xml_string, force_list=('product', 'tag'))
# ===== Using defusedxml for security =====
# pip install defusedxml
import defusedxml.ElementTree as ET
# Safe parsing - blocks XXE, entity expansion, etc.
tree = ET.fromstring(xml_string)
def element_to_dict(element):
result = {}
# Handle attributes
if element.attrib:
for key, value in element.attrib.items():
result[f'@{key}'] = value
# Handle child elements
children = list(element)
if children:
for child in children:
child_data = element_to_dict(child)
if child.tag in result:
# Convert to list for repeated elements
if not isinstance(result[child.tag], list):
result[child.tag] = [result[child.tag]]
result[child.tag].append(child_data)
else:
result[child.tag] = child_data
elif element.text and element.text.strip():
if result: # Has attributes
result['#text'] = element.text.strip()
else:
return element.text.strip()
return result
root = tree
json_data = {root.tag: element_to_dict(root)}
print(json.dumps(json_data, indent=2))
# ===== Using lxml with XPath =====
# pip install lxml
from lxml import etree
tree = etree.fromstring(xml_string.encode())
# Extract specific data with XPath, output as JSON
products = []
for product in tree.xpath('//product'):
products.append({
'id': product.get('id'),
'name': product.xpath('name/text()')[0],
'price': float(product.xpath('price/text()')[0]),
'currency': product.xpath('price/@currency')[0],
})
print(json.dumps(products, indent=2))Bash / CLI:XMLからJSON
xq(yqから)とxmlstarletでコマンドライン変換が可能です:
# ===== Using xq (part of yq, recommended) =====
# Install: pip install yq OR brew install yq
# Basic XML to JSON conversion
cat data.xml | xq .
# Or directly from a file
xq . data.xml
# Pretty-print with specific fields
xq '.catalog.product[] | {name: .name, price: .price}' data.xml
# Convert and save to file
xq . input.xml > output.json
# Extract specific values
xq -r '.catalog.product.name' data.xml
# ===== Using xmlstarlet =====
# Install: brew install xmlstarlet OR apt install xmlstarlet
# Select specific elements
xmlstarlet sel -t -v "//product/name" data.xml
# Convert to a flat key-value format
xmlstarlet sel -t \
-m "//product" \
-v "@id" -o "," \
-v "name" -o "," \
-v "price" -n data.xml
# ===== Python one-liners =====
# Quick XML to JSON from command line
python3 -c "
import xmltodict, json, sys
print(json.dumps(xmltodict.parse(sys.stdin.read()), indent=2))
" < data.xml
# Using built-in xml.etree (no pip install needed)
python3 -c "
import xml.etree.ElementTree as ET, json, sys
root = ET.parse(sys.stdin).getroot()
def to_dict(el):
d = dict(el.attrib)
children = list(el)
if children:
for c in children:
cd = to_dict(c)
if c.tag in d:
if not isinstance(d[c.tag], list): d[c.tag] = [d[c.tag]]
d[c.tag].append(cd)
else: d[c.tag] = cd
elif el.text and el.text.strip():
if d: d['#text'] = el.text.strip()
else: return el.text.strip()
return d
print(json.dumps({root.tag: to_dict(root)}, indent=2))
" < data.xml
# ===== Using curl + xq for API responses =====
# Fetch XML API and convert to JSON
curl -s "https://api.example.com/data.xml" | xq .
# SOAP response to JSON
curl -s -X POST "https://api.example.com/soap" \
-H "Content-Type: text/xml" \
-d @request.xml | xq '.Envelope.Body'Java:XMLからJSON
JavaではJackson XMLとorg.jsonが主要ツールです:
// ===== Using org.json (simple conversion) =====
// Maven: org.json:json:20231013
import org.json.JSONObject;
import org.json.XML;
public class XmlToJsonExample {
public static void main(String[] args) {
String xml = """
<bookstore>
<book id="1">
<title>Clean Code</title>
<author>Robert C. Martin</author>
<price>32.99</price>
</book>
</bookstore>
""";
// Simple one-line conversion
JSONObject json = XML.toJSONObject(xml);
System.out.println(json.toString(2));
// With configuration
JSONObject jsonKeepStrings = XML.toJSONObject(xml, true);
// true = keep all values as strings (no type coercion)
}
}
// ===== Using Jackson XML (more control) =====
// Maven: com.fasterxml.jackson.dataformat:jackson-dataformat-xml
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class JacksonXmlExample {
public static void main(String[] args) throws Exception {
String xml = "<book id=\"1\"><title>Clean Code</title></book>";
XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree(xml.getBytes());
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(node);
System.out.println(json);
}
}
// ===== Secure XML parsing in Java =====
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.XMLConstants;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// Prevent XXE attacks
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
dbf.setFeature(
"http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature(
"http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature(
"http://xml.org/sax/features/external-parameter-entities", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);JSONからXML変換
逆方向には固有の課題があります:
主な課題:JSON配列にはXMLの直接的な等価物がありません。null値には表現が必要です。プロパティ名がXMLで無効な場合があります。
JavaScriptとPythonでのJSONからXML変換の例:
// ===== JavaScript: JSON to XML =====
import { XMLBuilder } from 'fast-xml-parser';
const jsonData = {
catalog: {
product: [
{
'@_id': '101',
name: 'Wireless Mouse',
price: { '@_currency': 'USD', '#text': '29.99' },
tags: { tag: ['wireless', 'mouse'] },
},
{
'@_id': '102',
name: 'Keyboard',
price: { '@_currency': 'USD', '#text': '49.99' },
tags: { tag: ['keyboard', 'mechanical'] },
},
],
},
};
const builder = new XMLBuilder({
ignoreAttributes: false,
attributeNamePrefix: '@_',
textNodeName: '#text',
format: true, // pretty print
indentBy: ' ',
suppressEmptyNode: true,
});
const xml = builder.build(jsonData);
console.log(xml);
# ===== Python: JSON to XML =====
import xmltodict
json_data = {
'catalog': {
'product': {
'@id': '101',
'name': 'Wireless Mouse',
'price': {'@currency': 'USD', '#text': '29.99'},
}
}
}
xml_output = xmltodict.unparse(json_data, pretty=True)
print(xml_output)
# Output:
# <?xml version="1.0" encoding="utf-8"?>
# <catalog>
# <product id="101">
# <name>Wireless Mouse</name>
# <price currency="USD">29.99</price>
# </product>
# </catalog>エッジケースの処理
本番品質の変換器は多数のエッジケースを処理する必要があります:
XML属性:最も一般的なエッジケースで、プレフィックス規約が必要。
CDATAセクション:パースされないコンテンツを通常のテキストとして処理。
名前空間:マッピングの複雑さを増加。
混合コンテンツ:テキストと子要素を含む要素。
空要素:null、空文字列、空オブジェクトに変換可能。
配列検出:JSON配列を使用するタイミングの判断。
空白処理:有意な空白の区別。
XML宣言とDTD:JSONにマッピングされないメタデータ。
// Edge case examples: XML to JSON
// 1. Attributes + text content
// XML: <price currency="USD">29.99</price>
// JSON: { "price": { "@currency": "USD", "#text": "29.99" } }
// 2. CDATA section
// XML: <script><![CDATA[if (a < b) { alert("hello"); }]]></script>
// JSON: { "script": "if (a < b) { alert(\"hello\"); }" }
// 3. Namespaces
// XML: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
// <soap:Body><GetPrice><Item>Apple</Item></GetPrice></soap:Body>
// </soap:Envelope>
// JSON: { "soap:Envelope": { "soap:Body": { "GetPrice": { "Item": "Apple" } } } }
// 4. Mixed content
// XML: <p>Hello <b>world</b> today</p>
// JSON: { "p": { "#text": ["Hello ", " today"], "b": "world" } }
// 5. Self-closing / empty elements
// XML: <br/> OR <item></item>
// JSON: { "br": null } OR { "item": "" }
// 6. Single vs multiple children (array detection)
// XML (one child): <items><item>A</item></items>
// JSON (no array): { "items": { "item": "A" } }
// XML (two children):<items><item>A</item><item>B</item></items>
// JSON (array): { "items": { "item": ["A", "B"] } }
// Solution: use isArray option in fast-xml-parser or force_list in xmltodict
// 7. Whitespace preservation
// XML: <code xml:space="preserve"> hello world </code>
// JSON: { "code": " hello world " }
// 8. XML declaration (dropped in JSON)
// XML: <?xml version="1.0" encoding="UTF-8"?>
// JSON: (not included in output)XMLセキュリティベストプラクティス
XMLパーサーを使用する際、セキュリティは最も重要です:
XXE攻撃:外部エンティティ処理を無効にしてください。
Billion Laughs攻撃:エンティティ展開を制限してください。
DTDベースのインジェクション:外部DTDの読み込みを無効に。
セキュアな設定:各プログラミング言語でXMLパーサーの明示的なセキュリティ設定が必要です。
// ===== Secure XML parsing examples =====
// --- Python: Use defusedxml ---
# UNSAFE: xml.etree.ElementTree (vulnerable to XXE)
import xml.etree.ElementTree as ET # DO NOT use with untrusted XML
# SAFE: defusedxml blocks all known XML attacks
import defusedxml.ElementTree as SafeET
tree = SafeET.fromstring(untrusted_xml) # Safe!
# defusedxml blocks:
# - XML External Entity (XXE) attacks
# - Billion Laughs (entity expansion) attacks
# - External DTD retrieval
# - Decompression bombs
# --- Java: Secure DocumentBuilderFactory ---
import javax.xml.parsers.DocumentBuilderFactory;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// Disable all dangerous features
dbf.setFeature(
"http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature(
"http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature(
"http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature(
"http://apache.org/xml/features/nonvalidating/load-external-dtd",
false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
// --- JavaScript (Node.js): fast-xml-parser ---
import { XMLParser } from 'fast-xml-parser';
const secureParser = new XMLParser({
// fast-xml-parser does NOT process entities by default (safe)
processEntities: false, // explicitly disable
htmlEntities: false, // don't process HTML entities
allowBooleanAttributes: false,
});
// --- .NET: Secure XmlReaderSettings ---
// XmlReaderSettings settings = new XmlReaderSettings();
// settings.DtdProcessing = DtdProcessing.Prohibit;
// settings.XmlResolver = null;
// === Example: XXE attack payload (for awareness) ===
// This malicious XML attempts to read /etc/passwd:
//
// <?xml version="1.0"?>
// <!DOCTYPE data [
// <!ENTITY xxe SYSTEM "file:///etc/passwd">
// ]>
// <data>&xxe;</data>
//
// A vulnerable parser would include file contents in output.
// A secure parser rejects the DOCTYPE declaration entirely.
// === Example: Billion Laughs payload ===
// This XML expands to ~3 GB of text from a few hundred bytes:
//
// <?xml version="1.0"?>
// <!DOCTYPE lolz [
// <!ENTITY lol "lol">
// <!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
// <!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
// ...
// ]>
// <data>&lol9;</data>よくある質問
XMLをJSONに変換する最良の方法は?
言語によります。JavaScript:fast-xml-parser。Python:xmltodict。CLI:xq。素早い変換にはオンラインツールをご利用ください。
XML属性はJSON変換でどう処理されますか?
属性は@などのプレフィックスでマッピングされます。属性とテキストが共存する場合、テキストは#textキーに格納されます。
XMLからJSON変換は安全ですか?
適切な設定がないとXMLパースは危険です。外部エンティティを無効にし、Pythonではdefusedxmlを使用し、パーサーを安全に設定してください。
XMLからJSON変換は現代の開発者にとって基本的なスキルです。無料オンラインツールで素早く変換を行えます。