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"?>

<data

Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
1<
Total instance of Particular node attribute is :  2
3
Total instance of Particular node attribute is :  2
4=
Total instance of Particular node attribute is :  2
6
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<<?0<?1<?0
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<<?6<?7<?6
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<xml2xml3xml2
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<xml8
Total instance of Particular node attribute is :  2
4=version1 version2=version4version5

Total instance of Particular node attribute is :  2
8<xml8
Total instance of Particular node attribute is :  2
4==1 version2==4version5

Total instance of Particular node attribute is :  2
1=7
Total instance of Particular node attribute is :  2
3
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
1<
Total instance of Particular node attribute is :  2
3
Total instance of Particular node attribute is :  2
4="1.0"5
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<<?0?>0<?0
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<<?6?>6<?6
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<xml2<2xml2
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<xml8
Total instance of Particular node attribute is :  2
4=version1 version2=data3version5

Total instance of Particular node attribute is :  2
1=7
Total instance of Particular node attribute is :  2
3
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
1<
Total instance of Particular node attribute is :  2
3
Total instance of Particular node attribute is :  2
4=
Total instance of Particular node attribute is :  2
04
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<<?0
Total instance of Particular node attribute is :  2
09<?0
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<<?6
Total instance of Particular node attribute is :  2
15<?6
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<xml2
Total instance of Particular node attribute is :  2
21xml2
Total instance of Particular node attribute is :  2
0

Total instance of Particular node attribute is :  2
8<xml8
Total instance of Particular node attribute is :  2
4=
Total instance of Particular node attribute is :  2
6 version2==4version5

Total instance of Particular node attribute is :  2
1=7
Total instance of Particular node attribute is :  2
3
Total instance of Particular node attribute is :  2
0

=7data

Total instance of Particular node attribute is :  2
0

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 :  2
42
Total instance of Particular node attribute is :  2
43

 

 

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 :  2
46=
Total instance of Particular node attribute is :  2
6
Total instance of Particular node attribute is :  2
49

 

Total instance of Particular node attribute is :  2
_50

Total instance of Particular node attribute is :  2
51=
Total instance of Particular node attribute is :  2
53
Total instance of Particular node attribute is :  2
54
Total instance of Particular node attribute is :  2
55

Total instance of Particular node attribute is :  2
56=
Total instance of Particular node attribute is :  2
58

 

Total instance of Particular node attribute is :  2
_59

Total instance of Particular node attribute is :  2
60=
Total instance of Particular node attribute is :  2
62
Total instance of Particular node attribute is :  2
49

Total instance of Particular node attribute is :  2
64
Total instance of Particular node attribute is :  2
65
Total instance of Particular node attribute is :  2
66
Total instance of Particular node attribute is :  2
67
Total instance of Particular node attribute is :  2
68
Total instance of Particular node attribute is :  2
69

Total instance of Particular node attribute is :  2
1
Total instance of Particular node attribute is :  2
71

Total instance of Particular node attribute is :  2
1
Total instance of Particular node attribute is :  2
73
Total instance of Particular node attribute is :  2
74
Total instance of Particular node attribute is :  2
75
Total instance of Particular node attribute is :  2
55==
Total instance of Particular node attribute is :  2
79

Total instance of Particular node attribute is :  2
8
Total instance of Particular node attribute is :  2
60
Total instance of Particular node attribute is :  2
82=
Total instance of Particular node attribute is :  2
84
Total instance of Particular node attribute is :  2
49

 

Total instance of Particular node attribute is :  2
_86

Total instance of Particular node attribute is :  2
87
Total instance of Particular node attribute is :  2
88
Total instance of Particular node attribute is :  2
89
Total instance of Particular node attribute is :  2
90

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)