Stack Exchange
Get data from XML using xml.etree - python
I was wondering if anyone could help me with a situation, I am trying to get some values from an xml request. But, I was not successful, if anyone has any idea about it.
Please share it with me....
This is my code so far,
cost_centre = []

for book in xml.iter("unit"):
    try:
        if book.attrib["parentCode"] == 'MHI' or 'MHEU':
#             print(book.attrib['parentCode'])
#               print(book.attrib)
            if book.attrib["categoryName"] == "Cost Centre":
                cost_centre.append((book.attrib['parentCode'],book.attrib['oracleCode']))
    except:
        book = ''
# print(cost_centre)
df = pd.DataFrame(cost_centre,columns=['entity','oracleCode'])
display(df)

   entity   oracleCode
0   00174   STR-0134
1   00126   1120100101
# -------------------------------

What I want is...
   entity   oracleCode
0   MHI     STR-0134
1   MHI     1120100101


First, there is no element with a oracleCode attribute whose value is 1120100101. More importantly, it's not clear how you get to the element with a parentCode attribute equal to MHI as you have in your dataframe; is that manually entered?

I solved the problem!

#Convert to XML
xml = ET.fromstring(emberRaw)
cost_centre = []

for book in xml.iter("unit"):
    if book.get("parentCode") == 'MHI' or book.get("parentCode") == 'MHEU':
        mhi_mheu =  book.get("parentCode")
    if book.get("categoryName") == "Cost Centre":
        oracle = book.get("oracleCode")
    cost_centre.append((mhi_mheu,oracle))
    if cost_centre == '':
        cost_centre.remove()
        
cost_centre = list(set(cost_centre))
print(len(cost_centre))

df = pd.DataFrame(cost_centre,columns=['entity','oracleCode'])
display(df)

 entity    oracleCode
0   MHI    9999999987
1   MHI    1220200201
2   MHI    1110100102
3   MHEU   1220301804


