การเข้ารหัส
XML Entities Encode/Decode
เข้ารหัสและถอดรหัส XML entities — 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 Entities: การ escape อักขระในเอกสาร XML
คำถามที่พบบ่อย
เอนทิตี XML ที่กำหนดไว้ล่วงหน้า 5 ตัวคืออะไร?
XML กำหนดเอนทิตีแบบชื่อไว้ล่วงหน้าเพียงห้าตัวเท่านั้น: & (แอมเปอร์แซนด์), < และ > (วงเล็บมุม), ' (อะพอสทรอฟี) และ " (เครื่องหมายคำพูด) นอกเหนือจากนี้ต้องใช้การอ้างอิงอักขระแบบตัวเลขแทน
สิ่งนี้ต่างจากการเข้ารหัสเอนทิตี HTML อย่างไร?
HTML กำหนดชุดเอนทิตีแบบชื่อที่ใหญ่กว่ามาก (เช่น หรือ ©) ในขณะที่ XML ที่เข้มงวดรู้จักเพียงห้าตัวที่กำหนดไว้ล่วงหน้าเท่านั้น อักขระพิเศษอื่นใดใน XML ต้องใช้การอ้างอิงตัวเลขอย่าง © หรือ ©
เมื่อไหร่ที่ต้องหลีกเอนทิตี XML?
หลีกอักขระอย่าง < > & ' " ทุกครั้งที่ปรากฏในเนื้อหาข้อความหรือค่าแอตทริบิวต์ของเอกสาร XML เพื่อไม่ให้ตัวแยกวิเคราะห์เข้าใจผิดว่าเป็นมาร์กอัปและแปลไฟล์ไม่สำเร็จ
CDATA คืออะไร และควรใช้แทนเอนทิตีเมื่อไหร่?
ส่วน <![CDATA[...]]> ช่วยให้แทรกบล็อกข้อความโดยไม่ต้อง escape < และ & ได้ — สะดวกสำหรับส่วนของโค้ดหรือ HTML ขนาดใหญ่ภายใน XML ข้อยกเว้นคือลำดับ ]]> ซึ่งใช้ปิด CDATA และไม่สามารถปรากฏอยู่ในเนื้อหาเองได้
ตัวแยกวิเคราะห์ XML จัดการเอนทิตีที่ไม่ถูกต้องเหมือนกันทุกตัวหรือไม่?
ไม่ และต่างจากเบราว์เซอร์ที่ "ให้อภัย" ข้อผิดพลาดใน HTML ตัวแยกวิเคราะห์ XML เข้มงวด: เครื่องหมาย & หรือวงเล็บมุมที่ไม่ได้ escape เพียงตัวเดียวก็ทำให้เอกสารทั้งหมดไม่ถูกต้อง และการประมวลผลจะหยุดลงพร้อมข้อผิดพลาด