XML : how to read an xml into a table in python 3 using ElementTree

I have an xml file as below and I want to read it using ElementTree (ET) and the result I am looking for is a table,(i want the code to be iterable since the xml is much bigger thatn this and contains various objects, parameters, nodes, and values... (I have searched the site for possible solutions but that one is looking for some attributes while my xml structure is different and only contains tags and no attribute and also mine needs iteration) The xml:

  <?xml version="1.0"?>  <?xml-stylesheet type="text/xsl" href="MeasDataCollection.xsl"?>  <!DOCTYPE mdc SYSTEM "MeasDataCollection.dtd">  <mdc xmlns:HTML="http://www.w3.org/TR/REC-xml">      <md>          <neid>          <neun>Node1</neun>          <nedn>ParentObject</nedn>          <nesw>CXP90</nesw>      </neid>      <mi>          <mts>time</mts>          <gp>900</gp>          <mt>parameter1</mt>          <mt>parameter2</mt>          <mt>parameter3</mt>          <mv>              <moid>object1</moid>              <r>value1</r>              <r>value2</r>              <r>value3</r>          </mv>          <mv>              <moid>object2</moid>              <r>value4</r>              <r>value5</r>              <r>value6</r>          </mv>      </mi>      </md>  </mdc>    

and the output expected:

  Node1  ParentObject  time  parameter1  object1  value1  Node1  ParentObject  time  parameter2  object1  value2  Node1  ParentObject  time  parameter3  object1  value3  Node1  ParentObject  time  parameter1  object2  value4  Node1  ParentObject  time  parameter2  object2  value5  Node1  ParentObject  time  parameter3  object2  value6    

the code i have written so far is:(I know it is not complete)

  import xml.etree.cElementTree as ET  tree = ET.ElementTree(file='a.xml')  root=tree.getroot()  print(root, root.tag, root.attrib)  for child in tree.iterfind('Object1'):      print(child.tag, child.attrib, child.text)    

No comments:

Post a Comment