0

I am new to xml files and I am trying to read a xml file using 'ElementTree' as below:

import xml.etree.ElementTree as ET
tree = ET.parse('/users/user/file.xml')
root = tree.getroot()
a= [elem.tag for elem in root.iter()]

Using the above code I am able to return a list of columns present in the xml file. I am stuck and I am trying to do the below:

  1. How could I fetch the values corresponding to each column
  2. Loop in through the entire file to read the contents of the xml file.

Could anyone advice how could I move forward in this. Thanks..

Sample data:

<?xml version="1.0" encoding="UTF-8"?><Sales>
  <Sale>
    <Action>new</Action>
    <bill>46610272</bill>
    <prod_id/>
    <prod_name>
      <prod_cat/>
      <code>XYZ</code>
     </prod_name>
    <comments>
     </Sale>
 <Sale>
<Action>new</Action>
<bill>54465334</bill>
<prod_id/>
<prod_name>
  <prod_cat/>
  <code>ABC</code>
</prod_name>
<comments>
 </Sale>

Expected output:

Action,Bill,prod_id,prod_name,prod_cat,code,comments
new, 46610272,,,,XYZ,
new, 54465334,,,,XYZ,
scott martin
  • 1,253
  • 1
  • 14
  • 36

0 Answers0