Cara menambahkan atribut di xml menggunakan python

Pada artikel ini, kita akan melihat bagaimana mem-parsing XML dan menghitung instance dari atribut node tertentu dengan Python

Apa itu XML?

Extensible Markup Language (XML) Extensible Markup Language (XML) adalah bahasa markup yang menetapkan seperangkat aturan untuk menyandikan dokumen dalam format yang dapat dibaca oleh manusia dan dapat dibaca oleh mesin. Ini adalah bahasa markup seperti HTML, dan dirancang untuk menyimpan dan mengangkut data. Di sini, kita akan menggunakan modul XML bawaan di python untuk mem-parsing XML dan kemudian menghitung instance dari sebuah node. Kami menggunakan ElementTree XML API dan minidom API untuk mengurai file XML kami

Kode XML untuk catatan diberikan di bawah ini

Itu harus disimpan sebagai country_data. xml di direktori yang sama

XML




<?xml version="1.0"?>

<dataTotal instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 21<Total instance of Particular node attribute is : 23 Total instance of Particular node attribute is : 24=Total instance of Particular node attribute is : 26Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<<?0<?1<?0Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<<?6<?7<?6Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<xml2xml3xml2Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<xml8 Total instance of Particular node attribute is : 24=version1 version2=version4version5

Total instance of Particular node attribute is : 28<xml8 Total instance of Particular node attribute is : 24==1 version2==4version5

Total instance of Particular node attribute is : 21=7Total instance of Particular node attribute is : 23Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 21<Total instance of Particular node attribute is : 23 Total instance of Particular node attribute is : 24="1.0"5Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<<?0?>0<?0Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<<?6?>6<?6Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<xml2<2xml2Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<xml8 Total instance of Particular node attribute is : 24=version1 version2=data3version5

Total instance of Particular node attribute is : 21=7Total instance of Particular node attribute is : 23Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 21<Total instance of Particular node attribute is : 23 Total instance of Particular node attribute is : 24=Total instance of Particular node attribute is : 204Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<<?0Total instance of Particular node attribute is : 209<?0Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<<?6Total instance of Particular node attribute is : 215<?6Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<xml2Total instance of Particular node attribute is : 221xml2Total instance of Particular node attribute is : 20

Total instance of Particular node attribute is : 28<xml8 Total instance of Particular node attribute is : 24=Total instance of Particular node attribute is : 26 version2==4version5

Total instance of Particular node attribute is : 21=7Total instance of Particular node attribute is : 23Total instance of Particular node attribute is : 20

=7dataTotal instance of Particular node attribute is : 20

Contoh 1.  

Dalam contoh ini, Kami akan menggunakan xml. etree. Modul ElementTree untuk mem-parsing file XML kita dan menyimpannya dalam variabel pohon dan setelah itu kita akan menemukan semua contoh atribut node tertentu dengan fungsi python findall() dari modul ini. Sekarang kita mengulangi daftar dan memeriksa nilai atribut simpul tertentu jika cocok maka kita akan menambah hitungan sebagai 1 ke variabel kita

Python3




Total instance of Particular node attribute is : 2_41

Total instance of Particular node attribute is : 242 Total instance of Particular node attribute is : 243

 

 

Total instance of Particular node attribute is : 2_44

Total instance of Particular node attribute is : 2_45

Total instance of Particular node attribute is : 246= Total instance of Particular node attribute is : 26Total instance of Particular node attribute is : 249

 

Total instance of Particular node attribute is : 2_50

Total instance of Particular node attribute is : 251= Total instance of Particular node attribute is : 253Total instance of Particular node attribute is : 254Total instance of Particular node attribute is : 255

Total instance of Particular node attribute is : 256= Total instance of Particular node attribute is : 258

 

Total instance of Particular node attribute is : 2_59

Total instance of Particular node attribute is : 260= Total instance of Particular node attribute is : 262 Total instance of Particular node attribute is : 249

Total instance of Particular node attribute is : 264 Total instance of Particular node attribute is : 265Total instance of Particular node attribute is : 266 Total instance of Particular node attribute is : 267Total instance of Particular node attribute is : 268Total instance of Particular node attribute is : 269

Total instance of Particular node attribute is : 21Total instance of Particular node attribute is : 271

Total instance of Particular node attribute is : 21Total instance of Particular node attribute is : 273 Total instance of Particular node attribute is : 274Total instance of Particular node attribute is : 275Total instance of Particular node attribute is : 255== Total instance of Particular node attribute is : 279

Total instance of Particular node attribute is : 28Total instance of Particular node attribute is : 260Total instance of Particular node attribute is : 282=Total instance of Particular node attribute is : 284Total instance of Particular node attribute is : 249

 

Total instance of Particular node attribute is : 2_86

Total instance of Particular node attribute is : 287 Total instance of Particular node attribute is : 288Total instance of Particular node attribute is : 289Total instance of Particular node attribute is : 290

Keluaran

total instance of given node attribute is : 1

Contoh 2

Dalam contoh ini, kami akan mem-parsing file XML kami dengan bantuan modul minidom dan menetapkan ini ke variabel doc, fungsi getElementsByTagName() mengembalikan daftar instance dari node tertentu. Sekarang kita mengulangi daftar dan memeriksa nilai atribut simpul tertentu jika cocok maka kita akan menambah hitungan sebagai 1 ke variabel kita

Bagaimana cara menambahkan atribut dalam file XML menggunakan Python?

Konstruktor untuk Element dan SubElement menyertakan **extra , yang menerima atribut sebagai argumen kata kunci . Ini memungkinkan Anda untuk menambahkan sejumlah atribut yang sewenang-wenang. Anda juga dapat menggunakan penggunaan. setel untuk menambahkan atribut ke elemen yang sudah ada sebelumnya.

Bagaimana cara menambahkan tag baru dalam XML menggunakan Python?

Cara Menulis Node XML .
Untuk menambahkan XML baru dan menambahkannya ke dokumen, kami menggunakan kode “doc. membuat elemen”
Kode ini akan membuat tag keterampilan baru untuk atribut baru kami "Big-data"
Tambahkan tag keahlian ini ke dalam dokumen anak pertama (karyawan)
Jalankan kode- tag baru "big data" akan muncul dengan daftar keahlian lainnya

Bagaimana cara mengubah nilai atribut XML dengan Python?

Solusi. Pertama kita mem-parsing file xml dan melakukan operasi berisi string untuk memeriksa apakah judul buku berisi "Python". Jika string berisi kata kunci, kami menggunakan metode set() untuk mengubah nilai atribut . Jalankan kode contoh di atas dan Anda akan melihat file xml asli telah dimodifikasi.

Bagaimana cara mendapatkan nilai tag dari XML dengan Python?

Gunakan xml. .
pohon = ElementTree. mengurai("sampel. xml")
akar = pohon. getroot()
anjing = akar. findall("anjing")
untuk anjing di anjing
cetak (anjing. teks)

Postingan terbaru

LIHAT SEMUA