XML เป็น YAML
แปลง XML เป็นรูปแบบ YAML
อินพุต XML
เอาต์พุต YAML
การแปลง XML เป็น YAML คืออะไร?
การแปลง XML เป็น YAML คือกระบวนการเปลี่ยนข้อมูลจาก Extensible Markup Language (XML) ให้เป็น YAML Ain't Markup Language (YAML) XML ใช้แท็กวงเล็บมุมพร้อม attribute เพื่ออธิบายข้อมูลแบบลำดับชั้น ในขณะที่ YAML แสดงโครงสร้างเดียวกันผ่านการย่อหน้าและคู่ key-value แบบข้อความธรรมดา การแปลง XML เป็น YAML ออนไลน์เป็นงานทั่วไปเมื่อย้ายการกำหนดค่าจากระบบที่ใช้ XML เป็นหลัก เช่น Java Spring, Maven หรือ .NET ไปยังแพลตฟอร์มที่ต้องการ YAML เช่น Kubernetes, Ansible, GitHub Actions และ Docker Compose
ทั้งสองรูปแบบมีโมเดลข้อมูลที่แตกต่างกัน XML ถือว่าทุกอย่างเป็นข้อความโดยค่าเริ่มต้นและอาศัยคำนิยาม schema (XSD, DTD) สำหรับการบังคับประเภท YAML มีประเภทข้อมูลพื้นฐาน ได้แก่ string, integer, float, boolean, null, sequence (array) และ mapping (object) ระหว่างการแปลง ค่าเช่น "true", "5432" และ "3.14" อาจถูกตีความเป็นประเภทพื้นฐานของ YAML แทนที่จะคงเป็น string ตัวแปลงที่รอบคอบจะใส่เครื่องหมายคำพูดให้ค่าเหล่านี้เพื่อรักษาการแสดงข้อความดั้งเดิมจาก XML ต้นฉบับ
XML ยังรองรับโครงสร้างที่ไม่มีสิ่งเทียบเท่าใน YAML ได้แก่ attribute, namespace, processing instruction, CDATA section และ comment การแปลงต้องเลือกรูปแบบการแสดง attribute (โดยทั่วไปใช้ key ที่มีขีดล่างนำหน้า เช่น _attr) และตัดสินใจว่าจะทิ้งหรือรวมส่วนที่เหลือ การเข้าใจข้อดีข้อเสียเหล่านี้ก่อนแปลงช่วยให้คุณเลือกเครื่องมือที่เหมาะสม กำหนดค่าได้ถูกต้อง และตรวจสอบว่าผลลัพธ์ YAML ตรงกับเจตนาของ XML เดิม
<server>
<host>db.example.com</host>
<port>5432</port>
<credentials admin="true">
<username>deploy</username>
<password>s3cret</password>
</credentials>
<options>
<option>ssl=true</option>
<option>timeout=30</option>
</options>
</server>server:
host: db.example.com
port: "5432"
credentials:
_admin: "true"
username: deploy
password: s3cret
options:
option:
- ssl=true
- timeout=30ทำไมต้องใช้ตัวแปลง XML เป็น YAML ออนไลน์?
การเขียน script แปลงเองต้องจัดการกับการแมป attribute การตรวจจับ array สำหรับ element ที่ซ้ำกัน และ edge case ของการแปลงประเภทใน YAML ตัวแปลงบนเบราว์เซอร์จัดการทั้งหมดนั้นในขั้นตอนเดียว ทำให้คุณตรวจสอบผลลัพธ์ YAML และคัดลอกไปยังไฟล์กำหนดค่าได้โดยตรง
กรณีการใช้งานการแปลง XML เป็น YAML
ตารางอ้างอิงการแมป XML เป็น YAML
XML และ YAML มีโมเดลข้อมูลที่แตกต่างกัน ตารางด้านล่างแสดงว่าโครงสร้าง XML แต่ละแบบแมปไปยัง YAML ที่เทียบเท่าได้อย่างไร Attribute มักถูกแปลงเป็น key ที่มีขีดล่างนำหน้า และ element ที่ซ้ำกันจะกลายเป็น YAML sequence โครงสร้างบางอย่างเช่น comment และ processing instruction ไม่มีการแสดงผลใน YAML และถูกทิ้งระหว่างการแปลง
| โครงสร้าง XML | ตัวอย่าง XML | YAML ที่เทียบเท่า |
|---|---|---|
| Element | <name>text</name> | name: text |
| Nested elements | <a><b>1</b></a> | a:\n b: "1" |
| Attributes | <el attr="v"/> | el:\n _attr: v |
| Text + attributes | <el a="1">text</el> | el:\n _a: "1"\n _text: text |
| Repeated elements | <r><i>1</i><i>2</i></r> | r:\n i:\n - "1"\n - "2" |
| Empty element | <el/> | el: "" |
| CDATA | <![CDATA[raw]]> | Treated as plain text |
| Comments | <!-- note --> | Discarded (no YAML equivalent) |
| Namespaces | xmlns:ns="uri" | Prefix preserved or stripped |
| Boolean-like text | <flag>true</flag> | flag: "true" (quoted to stay string) |
XML กับ YAML: ความแตกต่างของโมเดลข้อมูล
การแปลงระหว่าง XML และ YAML ไม่ใช่แค่การสลับ syntax เท่านั้น รูปแบบทั้งสองมีความแตกต่างเชิงโครงสร้างพื้นฐานที่ส่งผลต่อการแสดงข้อมูลหลังการแปลง
ตัวอย่างโค้ด
ด้านล่างคือตัวอย่างที่ใช้งานได้จริงสำหรับการแปลง XML เป็น YAML ใน JavaScript, Python, Go และ command line แต่ละตัวอย่างรองรับ element ที่ซ้อนกัน, attribute และ sibling tag ที่ซ้ำกัน
import { parseStringPromise } from 'xml2js'
import YAML from 'yaml'
const xml = `
<config>
<database host="localhost" port="5432">
<name>mydb</name>
</database>
<features>
<feature>auth</feature>
<feature>logging</feature>
</features>
</config>`
const obj = await parseStringPromise(xml, { explicitArray: false })
console.log(YAML.stringify(obj))
// → config:
// → database:
// → $:
// → host: localhost
// → port: "5432"
// → name: mydb
// → features:
// → feature:
// → - auth
// → - loggingimport xmltodict
import yaml
xml = """
<server>
<host>db.example.com</host>
<port>5432</port>
<replicas>
<replica>node-1</replica>
<replica>node-2</replica>
</replicas>
</server>
"""
# Step 1: XML → Python dict
data = xmltodict.parse(xml)
# Step 2: Python dict → YAML
print(yaml.dump(data, default_flow_style=False))
# → server:
# → host: db.example.com
# → port: '5432'
# → replicas:
# → replica:
# → - node-1
# → - node-2
# With the standard library only (no xmltodict)
import xml.etree.ElementTree as ET
def elem_to_dict(elem):
d = {}
if elem.attrib:
d.update({f"_{k}": v for k, v in elem.attrib.items()})
for child in elem:
val = elem_to_dict(child)
if child.tag in d:
if not isinstance(d[child.tag], list):
d[child.tag] = [d[child.tag]]
d[child.tag].append(val)
else:
d[child.tag] = val
if elem.text and elem.text.strip():
text = elem.text.strip()
return text if not d else {**d, "_text": text}
return d
root = ET.fromstring(xml)
print(yaml.dump({root.tag: elem_to_dict(root)}, default_flow_style=False))# xq is part of the yq package (pip install yq) # It parses XML via xq and outputs JSON, then pipe to yq for YAML echo '<config><host>localhost</host><port>8080</port></config>' | xq . | yq -y . # → config: # → host: localhost # → port: "8080" # Using xmlstarlet + yq (Go version: https://github.com/mikefarah/yq) xmlstarlet sel -t -c '/' input.xml | yq -p=xml -o=yaml # Reads XML from file and outputs YAML directly
package main
import (
"encoding/xml"
"fmt"
"strings"
"gopkg.in/yaml.v3"
)
type Server struct {
XMLName xml.Name `xml:"server"`
Host string `xml:"host" yaml:"host"`
Port int `xml:"port" yaml:"port"`
Options []string `xml:"options>option" yaml:"options"`
}
func main() {
data := `<server>
<host>db.example.com</host>
<port>5432</port>
<options><option>ssl=true</option><option>timeout=30</option></options>
</server>`
var srv Server
xml.NewDecoder(strings.NewReader(data)).Decode(&srv)
out, _ := yaml.Marshal(srv)
fmt.Println(string(out))
// → host: db.example.com
// → port: 5432
// → options:
// → - ssl=true
// → - timeout=30
}