Pada artikel ini, kita akan melihat bagaimana mem-parsing XML dan menghitung instance dari atribut node tertentu dengan Python Show
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 iniItu harus disimpan sebagai country_data. xml di direktori yang sama XML
Total 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 : 26 Total 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 < xml 2xml 3xml 2Total instance of Particular node attribute is : 20 Total instance of Particular node attribute is : 28 < xml 8 Total instance of Particular node attribute is : 24 = version 1 version 2= version 4version 5Total instance of Particular node attribute is : 28 < xml 8 Total instance of Particular node attribute is : 24 = = 1 version 2= = 4version 5Total instance of Particular node attribute is : 21 = 7Total instance of Particular node attribute is : 23 Total 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 < xml 2< 2xml 2Total instance of Particular node attribute is : 20 Total instance of Particular node attribute is : 28 < xml 8 Total instance of Particular node attribute is : 24 = version 1 version 2= data 3version 5Total instance of Particular node attribute is : 21 = 7Total instance of Particular node attribute is : 23 Total 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 : 204 Total 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 < xml 2Total instance of Particular node attribute is : 221 xml 2Total instance of Particular node attribute is : 20 Total instance of Particular node attribute is : 28 < xml 8 Total instance of Particular node attribute is : 24 = Total instance of Particular node attribute is : 26 version 2= = 4version 5Total instance of Particular node attribute is : 21 = 7Total instance of Particular node attribute is : 23 Total instance of Particular node attribute is : 20
Total 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 Python3Total 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 : 26 Total 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 : 253 Total instance of Particular node attribute is : 254 Total 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 : 265 Total instance of Particular node attribute is : 266 Total instance of Particular node attribute is : 267 Total instance of Particular node attribute is : 268 Total instance of Particular node attribute is : 269 Total instance of Particular node attribute is : 21 Total instance of Particular node attribute is : 271 Total instance of Particular node attribute is : 21 Total instance of Particular node attribute is : 273 Total instance of Particular node attribute is : 274 Total instance of Particular node attribute is : 275 Total instance of Particular node attribute is : 255 = = Total instance of Particular node attribute is : 279 Total instance of Particular node attribute is : 28 Total instance of Particular node attribute is : 260 Total instance of Particular node attribute is : 282 = Total instance of Particular node attribute is : 284 Total 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 : 288 Total instance of Particular node attribute is : 289 Total instance of Particular node attribute is : 290 Keluaran total instance of given node attribute is : 1 Contoh 2Dalam 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) |