0

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)
John Smith
  • 123
  • 2
  • 2
  • 6
  • Possible duplicate of [Parsing XML with namespace in Python via 'ElementTree'](http://stackoverflow.com/questions/14853243/parsing-xml-with-namespace-in-python-via-elementtree) – styvane Nov 24 '15 at 21:04
  • @user3100115 the duplication you mentioned is not related to my question, the only common part in our question are xml and ET, – John Smith Nov 25 '15 at 10:22

0 Answers0