Saya mencoba mengekstrak elemen dari sistem pencatatan Parlemen Inggris, Hansard. Tapi xmlnya tidak diformat dengan baik. Misalnya, ia memiliki struktur sebagai berikut: housecommons/debates/section/section/data housecommons/speeches/section/section/data housecommons/section/section/data
Saya memiliki kode yang bisa mendapatkan data seperti contoh di bawah ini. Namun, hal ini menghasilkan nilai duplikat. Saya pikir alasannya adalah kode saya tidak menghormati hubungan induk anak dan malah hanya mencari setiap penanda 'bagian' yang mengikuti penanda pertama.
Bisakah seseorang memberi saran bagaimana saya dapat mengadaptasi kode saya untuk menghindari duplikat?
doc = ET.parse(xmlFile)
for house in doc.iter('housecommons'):
#print ('housecommon', house.tag)
for debates in house.iter('debates'):
print ('debates')
#print (title)
for section in debates.iter('section'):
#print ("debates section", section.text)
#title = ("title", section.find('title').text)
print('debate section')
for subsection in debates.iter('section'):
print("debate subsection", subsection.text)
#title = ("title", subsection.find('title').text)
#print(title)
n.b. ini adalah jenis hal dalam xml mentah yang saya coba urai. Tanpa memposting xml lengkap, struktur kompleks mungkin tidak sepenuhnya terlihat.
<housecommons>
<image src="S6CV0001P0I0103"/>
<col>181</col>
<title>House of Commons</title>
<date format="1981-03-17">Tuesday 17 March 1981</date>
<p id="S6CV0001P0-00854"><i>The House met at half-past Two o'clock</i></p>
<debates>
<section>
<title>PRAYERS</title>
<p id="S6CV0001P0-00855">[MR. SPEAKER <i>in the Chair</i>]</p>
<section>
<title>PRIVATE BUSINESS</title>
<section>
<title>CHARTERHOUSE JAPHET BILL</title>
<p id="S6CV0001P0-00856"><i>Bill read the Third time and passed, with amendments.</i></p>
<section>
<title>BARNSLEY BOROUGH COUNCIL BILL</title>
<p id="S6CV0001P0-00857"><i>Further considered; to be read the Third time.</i></p>
</section>
</section>
</section>
</section>
</housecommons>