Python XML Dosyası – Nasıl Okunur, Yazılır ve Ayrıştırılır

⚡ Akıllı Özet

Python XML processing lets you read, write, and parse XML documents using the built-in minidom and ElementTree modules. The minidom class loads a file into memory as a DOM, while ElementTree offers a faster, more Pythonic tree API.

  • 🔘 What is XML: eXtensible Markup Language stores and transports small, structured data.
  • ☑️ minidom parsing: The parse() function loads an XML file into memory as a DOM.
  • Reading tags: getElementsByTagName() returns matching elements; getAttribute() reads their values.
  • 🧪 Writing nodes: createElement() and appendChild() add a new tag to a document.
  • ElementTree API: ET.parse() builds a tree; getroot() returns the root element.
  • 🤖 Yapay zekâya hazır: Parsed XML feeds configs and API data into machine learning pipelines.

Python XML dosyası

The sections below cover parsing, writing, and reading XML files in Python.

XML nedir?

XML, Genişletilebilir İşaretleme Dili anlamına gelir. Küçük ve orta miktarlardaki verileri depolamak ve taşımak için tasarlanmıştır ve yapılandırılmış bilgilerin paylaşımında yaygın olarak kullanılır.

Python enables you to parse and modify XML documents. In order to parse an XML document, you need to have the entire XML document in memory. In this tutorial, we will see how we can use the XML minidom class in Python XML dosyalarını yüklemek ve ayrıştırmak için.

Minidom kullanarak XML Ayrıştırma nasıl yapılır

Ayrıştıracağımız örnek bir XML dosyası oluşturduk.

Adım 1) Örnek XML dosyası oluşturun

Dosyanın içinde adınızı, soyadınızı, evinizi ve uzmanlık alanınızı (SQL, Python, Testing and Business).

Minidom kullanarak XML Ayrıştırma nasıl yapılır

Adım 2) XML dosyasını yüklemek ve ayrıştırmak için ayrıştırma işlevini kullanın

Once we have parsed the document, we will print out the “node name” of the root of the document and the “firstchild tagname”. Tagname and nodename are the standard properties of the XML file.

Minidom kullanarak XML'i ayrıştırma

  • Import the xml.dom.minidom module and declare the file that has to be parsed (myxml.xml)
  • Bu dosya, bir çalışanın adı, soyadı, evi, uzmanlık alanı vb. gibi bazı temel bilgileri içerir.
  • XML dosyasını yüklemek ve ayrıştırmak için XML minidomundaki ayrıştırma işlevini kullanıyoruz
  • We have a variable doc, and doc gets the result of the parse function
  • We want to print the nodename and child tagname from the file, so we declare it in the print function
  • Kodu çalıştırın - XML ​​dosyasından düğüm adını (#document) ve XML dosyasından ilk alt etiket adını (çalışan) yazdırır.

not: Nodename and child tagname are the standard names or properties of an XML dom.

Step 3) Call the list of XML tags from the XML document and print it

Next, we can also call the list of XML tags from the XML document and print it. Here we printed out the set of skills like SQL, Python, Test yapmak ve İşletme.

Minidom kullanarak XML'i ayrıştırma

  • Declare the variable expertise, from which we are going to extract all the expertise names the employee has
  • “getElementsByTagName” adlı dom standart işlevini kullanın
  • Bu, beceri adı verilen tüm unsurları elde edecek
  • Declare a loop over each one of the skill tags
  • Run the code- It will give a list of four skills

XML Düğümü Nasıl Yazılır

“createElement” fonksiyonunu kullanarak yeni bir nitelik oluşturabilir ve daha sonra bu yeni niteliği veya etiketi mevcut XML etiketlerine ekleyebiliriz. XML dosyamıza yeni bir “BigData” etiketi ekledik.

  1. You have to write code to add the new attribute (BigData) to the existing XML tag
  2. Then, you have to print out the XML tag with the new attribute appended to the existing XML tag

XML Düğümü Yaz

  • To add a new XML tag and add it to the document, we use the code “doc.createElement”
  • This code will create a new skill tag for our new attribute “BigData”
  • Add this skill tag into the document’s first child (employee)
  • Run the code- the new tag “BigData” will appear with the other list of expertise

XML Ayrıştırıcı Örneği

Python 2 Örnek

import xml.dom.minidom

def main():
# use the parse() function to load and parse an XML file
   doc = xml.dom.minidom.parse("Myxml.xml");
  
# print out the document node and the name of the first child tag
   print doc.nodeName
   print doc.firstChild.tagName
  
# get a list of XML tags from the document and print each one
   expertise = doc.getElementsByTagName("expertise")
   print "%d expertise:" % expertise.length
   for skill in expertise:
     print skill.getAttribute("name")
    
#Write a new XML tag and add it into the document
   newexpertise = doc.createElement("expertise")
   newexpertise.setAttribute("name", "BigData")
   doc.firstChild.appendChild(newexpertise)
   print " "

   expertise = doc.getElementsByTagName("expertise")
   print "%d expertise:" % expertise.length
   for skill in expertise:
     print skill.getAttribute("name")
    
if name == "__main__":
  main();

Python 3 Örnek

import xml.dom.minidom

def main():
    # use the parse() function to load and parse an XML file
    doc = xml.dom.minidom.parse("Myxml.xml");

    # print out the document node and the name of the first child tag
    print (doc.nodeName)
    print (doc.firstChild.tagName)
    # get a list of XML tags from the document and print each one
    expertise = doc.getElementsByTagName("expertise")
    print ("%d expertise:" % expertise.length)
    for skill in expertise:
        print (skill.getAttribute("name"))

    # Write a new XML tag and add it into the document
    newexpertise = doc.createElement("expertise")
    newexpertise.setAttribute("name", "BigData")
    doc.firstChild.appendChild(newexpertise)
    print (" ")

    expertise = doc.getElementsByTagName("expertise")
    print ("%d expertise:" % expertise.length)
    for skill in expertise:
        print (skill.getAttribute("name"))

if __name__ == "__main__":
    main();

ElementTree kullanarak XML Ayrıştırma

ElementTree is an API for manipulating XML. ElementTree is an easy way to process XML files.

Örnek veri olarak aşağıdaki XML belgesini kullanıyoruz:

<data>
   <items>
      <item name="expertise1">SQL</item>
      <item name="expertise2">Python</item>
   </items>
</data>

ElementTree kullanarak XML okuma:

We must first import the xml.etree.ElementTree module.

import xml.etree.ElementTree as ET

Now let us fetch the root element:

root = tree.getroot()

Following is the complete code for reading the above XML data:

import xml.etree.ElementTree as ET
tree = ET.parse('items.xml')
root = tree.getroot()

# all items data
print('Expertise Data:')

for elem in root:
   for subelem in elem:
      print(subelem.text)

Çıktı:

Expertise Data:
SQL
Python

SSS

Use minidom for small files needing DOM navigation. Choose ElementTree for most work — it is faster, lighter, and the recommended, more Pythonic default.

Wrap the root in an ElementTree and call tree.write(“out.xml”, encoding=”utf-8″, xml_declaration=True). For indentation, use ET.indent() on Python 3.9+, or lxml’s pretty_print.

lxml is a third-party library sharing the ElementTree API but backed by fast C code. It adds XPath, XSLT, and validation. Install via pip install lxml.

Standard parsers are vulnerable to XXE and billion-laughs attacks. Install defusedxml and import defusedxml.ElementTree — a drop-in replacement that disables dangerous external entities.

Yes. Use minidom.parseString(text) or ElementTree’s ET.fromstring(text), which returns the root directly. Both parse XML from a variable, so responses need not be saved first.

Walk the tree with ElementTree, building a dict from each element’s tag, attributes, and text. The xmltodict library automates conversion via xmltodict.parse().

XML stores configs, annotations, and API responses that feed models. Pascal VOC image labels ship as XML; ElementTree extracts their bounding boxes for training.

Yes. GitHub Copilot autocompletes minidom and ElementTree patterns like parse() and findall() loops. Still verify tag names and encodings, and add defusedxml for untrusted input.

Bu yazıyı şu şekilde özetleyin: