エンコード
XML Entities Encode/Decode
XML エンティティのエンコード・デコード — 標準の5種類(アンパサンド、山括弧、アポストロフィ、引用符)と数値文字参照。
Unlike HTML, XML strictly requires escaping five special characters (&, <, >, ', "), or the document is considered invalid. This tool encodes and decodes those entities, as well as numeric character references.
How to use it
- Encode: paste text and the five reserved characters get replaced with their matching entities (&, <, >, ', ").
- Decode: paste XML containing entities to get plain text back.
- Numeric character references (& or &) are supported too, for characters outside the standard five.
Common uses
- Preparing user text for insertion into an XML document (an RSS feed, a SOAP request, a config file).
- Debugging why an XML parser rejects a document — often the cause is an unescaped ampersand or angle bracket inside the text.
- Decoding content received from an XML-based API to read it in its original form.
Things to keep in mind
XML parsers are much stricter than HTML: a single unescaped entity invalidates the entire document, not just one element.
CDATA sections (<![CDATA[...]]>) are an alternative to escaping for large blocks of text, letting you insert arbitrary content without replacing every special character individually.
よくある質問
XML エンティティと HTML エンティティは何が違いますか?
XMLが標準で定義している名前付きエンティティは<、>、&、'、"の5種類だけです。HTMLはこれよりはるかに多くの名前付きエンティティを持つため、XML文書では基本的にこの5種類と数値文字参照のみを使います。
なぜ XML ではこの5種類しかエスケープが必要ないのですか?
この5文字はXMLの構文(タグや属性の区切り)で特別な意味を持つため、そのまま出力するとパースエラーの原因になります。それ以外の文字は数値文字参照で表現するか、そのまま記述できます。
「基本」と「すべての文字」はどう使い分ければいいですか?
「基本」はXML構文上エスケープが必須な5文字のみを変換するため、通常のXML/HTML編集ではこちらで十分です。「すべての文字」は非ASCII文字なども数値文字参照に変換したい場合に使います。
CDATAとは何ですか、エンティティの代わりにいつ使うべきですか?
<![CDATA[...]]>セクションを使うと、<と&をエスケープせずにテキストブロックを挿入できます。XML内に埋め込む大きなコードやHTMLの断片に便利です。例外は]]>というシーケンスで、これはCDATAを閉じるため内容中には使えません。
XMLパーサーは不正なエンティティをどれも同じように扱いますか?
いいえ。HTMLを解釈するブラウザはエラーを「大目に見て」くれますが、XMLパーサーは厳格です。エスケープされていないアンパサンドや山括弧が1つでもあれば、文書全体が無効とみなされ処理はエラーで停止します。