0

I am trying to use Python to process a very large XML file, here's a sample file.

<FIXML r="20030618" s="20040109" v="4.4" xr="FIA" xv="1"
xmlns="http://www.fixprotocol.org/FIXML-4-4">
<Batch>
    <MktDataFull RptID="23520135" BizDt="2015-01-20">
        <Instrmt Sym="OEF" MMY="20171215" MatDt="2017-12-15" CFI="OCASPS" StrkPx="100" StrkMult="1" StrkValu="100" Mult="100" StrkCcy="USD"/>
        <Full Typ="5" Px="5.65" Ccy="USD" PxDelta="0.35" Dt="2015-01-20"/>
        <Full Typ="D" Px="89.29" Dt="2015-01-20"/>
    </MktDataFull>
    <MktDataFull RptID="16349083" BizDt="2015-01-20">
        <Instrmt Sym="OEF" MMY="20151219" MatDt="2015-12-19" CFI="OCASPS" StrkPx="35" StrkMult="1" StrkValu="100" Mult="100" StrkCcy="USD"/>
        <Full Typ="5" Px="54.3225" Ccy="USD" PxDelta="0.99" Dt="2015-01-20"/>
        <Full Typ="D" Px="89.29" Dt="2015-01-20"/>
    </MktDataFull>
</Batch>

What I am trying to do is for every XML element of the name MktDataFull, I want to print out a line of all attributes contain in that node plus child nodes. Here's my attempt just to print a few attributes, by finding all MktDataFull elements.

import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='file.xml')
for elem in tree.findall('MktDataFull'):
    print elem.attrib["RptID"]
    print elem.attrib["BizDt"]
    print elem.attrib["Sym"]
    # etc

I am returning no values for this however. Can someone help?

philshem
  • 24,761
  • 8
  • 61
  • 127
user2254180
  • 844
  • 13
  • 30
  • The `xmlns` declaration puts all of these elements in a namespace, so try for example `tree.findall('fix:MktDataFull', {'fix': 'http://www.fixprotocol.org/FIXML-4-4'})`. – John Kugelman Mar 11 '15 at 13:52
  • Tried that, getting the following error - Traceback (most recent call last): File "script.py", line 5, in for elem in tree.findall('fix:MktDataFull', namespace): TypeError: findall() takes exactly 2 arguments (3 given) – user2254180 Mar 11 '15 at 13:59
  • 1
    Try this one:`tree.findall('//{http://www.fixprotocol.org/FIXML-4-4}MktDataFull')` – Mika Mar 11 '15 at 14:09

0 Answers0