コンテンツへスキップ
uencoder

JSON を XML に変換する方法

JSON データを XML 形式に変換します。配列、ネストしたオブジェクト、属性を扱います。

问题

XML を期待するシステムにデータを送る必要がありますが、元データは JSON 形式です。

前提条件

  • JSON の理解
  • XML 構造の理解

ステップ

  1. 1. Define the conversion rules

    JSON objects become XML elements, keys become tag names, and values become element content. Arrays need special handling: each element becomes a repeated child tag. Decide on a root element name and how to represent arrays and null values.

    入力
    {"users":[{"name":"Alice"}]}
    输出
    rules defined
  2. 2. Perform the conversion

    Convert the JSON to XML following your rules. Nested objects become nested elements. Array items use the parent key as the tag name. Strings become text content; numbers and booleans become text content (XML has no native types).

    入力
    {"users":[{"name":"Alice"}]}
    输出
    <users><name>Alice</name></users>
  3. 3. Validate the XML output

    Parse the resulting XML to ensure it is well-formed. Check that all tags are closed, special characters are escaped (& < > are escaped as &amp; &lt; &gt;), and the encoding declaration is correct if needed.

    入力
    <users><name>Alice</name></users>
    输出
    well-formed XML ✓

期待される結果

元の JSON と同じデータを表す、整形式の XML ドキュメント。

よくあるエラー

XML tags start with a number or contain spaces
XML tag names must start with a letter or underscore and cannot contain spaces. Sanitize JSON keys before converting.
Special characters not escaped
Escape &, <, >, ", and ' as XML entities. The converter should handle this automatically.

トラブルシューティング指引

  • If the XML parser rejects the output, check for unescaped special characters and unclosed tags.
  • JSON null has no direct XML equivalent. Decide on a convention (empty element, xsi:nil attribute, or omission).

セキュリティ上の制限

  • XML can be vulnerable to XXE (XML External Entity) attacks if parsed with an unsafe parser. Use a parser that disables DTD and external entities.

関連ツール

参考链接