# เขียน Python อ่านข้อมูลจากไฟล์ XML

**URL:** <https://discuss.dataengineercafe.io/t/python-xml/400>\
**Category:** General Discussion เรื่อง Data Engineering\
**Tags:** python, xml\
**Created:** [September 3, 2022, 6:42am UTC](https://discuss.dataengineercafe.io/t/python-xml/400 "2022-09-03T06:42:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zkan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discuss.dataengineercafe.io/zkan/32/2_2.png) [@zkan](https://discuss.dataengineercafe.io/u/zkan)\
**Post date:** [September 3, 2022, 6:42am UTC](https://discuss.dataengineercafe.io/t/python-xml/400/1 "2022-09-03T06:42:03Z")

</div>

ในสายงานข้อมูลของเรา เราต้องไปเชื่อมต่อกับระบบที่หลากหลายมาก มีทั้งใหม่ทั้งเก่า ซึ่งการเก็บข้อมูล หรือการส่งข้อมูลระหว่างกัน หลาย ๆ ระบบยังมีใช้ XML อยู่บ้าง ซึ่งตรงนี้พวกเราดู ๆ ไว้หน่อยก็ไม่เสียหายครับ อิอิ

สมมุติว่าเรามีไฟล์ XML หน้าตาแบบด้านล่างนี้ ชื่อว่า `country.xml`

```xml
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

```

เราอยากจะเขียนโค้ด Python อ่านค่าต่าง ๆ ใน XML เราทำได้หลายวิธีมาก ขอเลือกใช้วิธี [The ElementTree XML API](https://docs.python.org/3.10/library/xml.etree.elementtree.html#module-xml.etree.ElementTree) มาทำให้ดู เพราะว่าไม่ต้องติดตั้งอะไรเพิ่ม

ก่อนอื่นเราก็ import ตัว module เข้ามา แล้วก็โยนไฟล์เข้าไปให้ module มันไปสร้างโครงสร้างต่าง ๆ กลับมาให้เรา

```python
import xml.etree.ElementTree as ET

tree = ET.parse("country.xml")
root = tree.getroot()

```

เท่านี้เราก็สามารถอ่านค่าต่าง ๆ จากข้อมูลใน XML ได้ล่ะ เช่น ถ้าเราอยากจะได้ค่า name ใน tag ชื่อ country เราก็เขียนลูปแบบนี้

```python
for child in root:
    print(child.tag, child.attrib)

```

ผลคือ

```auto
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}

```

ถ้าเราอยากจะได้ค่า neighbor เราก็เขียนแบบนี้

```python
for neighbor in root.iter("neighbor"):
    print(neighbor.attrib)

```

เราจะได้

```auto
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
{'name': 'Malaysia', 'direction': 'N'}
{'name': 'Costa Rica', 'direction': 'W'}
{'name': 'Colombia', 'direction': 'E'}

```

หรือถ้าเราอยากได้ค่า rank ในแต่ละ country เราก็เขียนแบบนี้

```python
for country in root.findall("country"):
    rank = country.find("rank").text
    name = country.get("name")
    print(name, rank)

```

ผลคือ

```auto
Liechtenstein 1
Singapore 4
Panama 68

```

สามารถอ่านเพิ่มเติมเกี่ยวกับวิธีการดึงข้อมูลจาก XML ได้ที่บทความด้านล่างนี้เลย เขียนไว้ละเอียดดี

> **[A Roadmap to XML Parsers in Python – Real Python](https://realpython.com/python-xml-parser/)**
>
> In this tutorial, you'll learn what XML parsers are available in Python and how to pick the right parsing model for your specific use case. You'll explore Python's built-in parsers as well as major third-party libraries.

ปล. โค้ดทั้งหมดอยู่ที่นี่ครับ 👉 [GitHub - zkan/hello-xml-parser](https://github.com/zkan/hello-xml-parser)

---

<div class="post-metadata">

**Author:** ![kahnwong](https://yyz1.discourse-cdn.com/flex035/user_avatar/discuss.dataengineercafe.io/kahnwong/32/295_2.png) [@kahnwong](https://discuss.dataengineercafe.io/u/kahnwong)\
**Post date:** [September 3, 2022, 7:20am UTC](https://discuss.dataengineercafe.io/t/python-xml/400/2 "2022-09-03T07:20:04Z")

</div>

มีอีกท่า [XML file | Databricks on AWS](https://docs.databricks.com/data/data-sources/xml.html#language-scala)

อันนี้แอบขี้โกง เพราะจริงๆ มันคือ scala/java ข้างใน แค่ยืม python เป็นตัวกลางให้คุยผ่าน 5555

แต่ยังดีที่ไม่ต้องอ่านไฟล์จาก ftp อันนี้กรี๊ดเลย 😆
