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.

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).
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.
- 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.
- 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.
- You have to write code to add the new attribute (BigData) to the existing XML tag
- Then, you have to print out the XML tag with the new attribute appended to the existing XML tag
- 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




