通过Python ElementTree查找XML树中的所有子节点

通过Python ElementTree查找XML树中的所有子节点,python,xml,elementtree,Python,Xml,Elementtree,给定下面的XML,如何“获取”每个OptnOnSpotAndFutrsInf“对象”(它们位于XML示例的底部),以便从“TckrSymb”和“CFICd”中提取文本 我正在尝试使用: import xml.etree.ElementTree as ET tree = ET.parse('file.xml') root = tree.getroot() for e in root.iter('OptnOnSpotAndFutrsInf'): print(e.text) 但它什么也没

给定下面的XML,如何“获取”每个OptnOnSpotAndFutrsInf“对象”(它们位于XML示例的底部),以便从“TckrSymb”和“CFICd”中提取文本

我正在尝试使用:

import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()

for e in root.iter('OptnOnSpotAndFutrsInf'):
     print(e.text)
但它什么也没回来

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:bvmf.052.01.xsd bvmf.052.01.xsd" xmlns="urn:bvmf.052.01.xsd">
  <BizFileHdr>
    <Xchg>
      <BizGrpDesc>
        <Fr>
          <OrgId>
            <Id>
              <OrgId>
                <Othr>
                  <Id>BVMF</Id>
                  <Issr>40</Issr>
                  <SchmeNm>
                    <Prtry>39</Prtry>
                  </SchmeNm>
                </Othr>
              </OrgId>
            </Id>
          </OrgId>
        </Fr>
        <To>
          <OrgId>
            <Id>
              <OrgId>
                <Othr>
                  <Id>PUBLIC</Id>
                  <Issr>40</Issr>
                  <SchmeNm>
                    <Prtry>39</Prtry>
                  </SchmeNm>
                </Othr>
              </OrgId>
            </Id>
          </OrgId>
        </To>
        <BizGrpDtls>
          <BizGrpIdr>BV000327201803190327120433334899343</BizGrpIdr>
          <TtlNbOfMsg>38302</TtlNbOfMsg>
          <BizGrpTp>BVBG.028.02</BizGrpTp>
          <CreDtAndTm>2018-03-19T18:18:20</CreDtAndTm>
        </BizGrpDtls>
        <MsgTpDef>
          <MsgDefIdr>bvmf.100.02</MsgDefIdr>
          <NbOfMsg>38302</NbOfMsg>
        </MsgTpDef>
      </BizGrpDesc>
      <BizGrp>
        <AppHdr xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
          <BizMsgIdr>BV000327201803190327106886457976417</BizMsgIdr>
          <MsgDefIdr>bvmf.100.02</MsgDefIdr>
          <CreDt>2018-03-19T21:18:06Z</CreDt>
          <Fr>
            <OrgId>
              <Id>
                <OrgId>
                  <Othr>
                    <Id>BVMF</Id>
                    <SchmeNm>
                      <Prtry>39</Prtry>
                    </SchmeNm>
                    <Issr>40</Issr>
                  </Othr>
                </OrgId>
              </Id>
            </OrgId>
          </Fr>
          <To>
            <OrgId>
              <Id>
                <OrgId>
                  <Othr>
                    <Id>PUBLIC</Id>
                    <SchmeNm>
                      <Prtry>39</Prtry>
                    </SchmeNm>
                    <Issr>40</Issr>
                  </Othr>
                </OrgId>
              </Id>
            </OrgId>
          </To>
        </AppHdr>
        <Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:bvmf.100.02.xsd">
          <Instrm>
            <RptParams>
              <ActvtyInd>true</ActvtyInd>
              <Frqcy>DAIL</Frqcy>
              <NetPosId>XXXX</NetPosId>
              <RptDtAndTm>
                <Dt>2018-03-19</Dt>
              </RptDtAndTm>
              <UpdTp>COMP</UpdTp>
            </RptParams>
            <FinInstrmId>
              <OthrId>
                <Id>200000162828</Id>
                <Tp>
                  <Prtry>8</Prtry>
                </Tp>
              </OthrId>
              <PlcOfListg>
                <MktIdrCd>BVMF</MktIdrCd>
              </PlcOfListg>
            </FinInstrmId>
            <FinInstrmAttrCmon>
              <Asst>HGLG</Asst>
              <AsstDesc>HGLG</AsstDesc>
              <Mkt>17</Mkt>
              <Sgmt>1</Sgmt>
              <Desc>FII CSHG LOGCI</Desc>
            </FinInstrmAttrCmon>
            <InstrmInf>
              <EqtyInf>
                <SctyCtgy>25</SctyCtgy>
                <ISIN>BRHGLGCTF004</ISIN>
                <DstrbtnId>190</DstrbtnId>
                <CFICd>CICIRU</CFICd>
                <SpcfctnCd>CI        </SpcfctnCd>
                <CrpnNm>CSHG LOGÍSTICA FDO INV IMOB - FII                 </CrpnNm>
                <TckrSymb>HGLG11L</TckrSymb>
                <PmtTp>1</PmtTp>
                <AllcnRndLot>1</AllcnRndLot>
                <PricFctr>1</PricFctr>
                <TradgStartDt>9999-12-31</TradgStartDt>
                <TradgEndDt>9999-12-31</TradgEndDt>
                <CorpActnStartDt>9999-12-31</CorpActnStartDt>
                <EXDstrbtnNb>0</EXDstrbtnNb>
                <CtdyTrtmntTp>0</CtdyTrtmntTp>
                <TradgCcy>BRL</TradgCcy>
                <MktCptlstn Ccy="BRL">671978</MktCptlstn>
                <LastPric Ccy="BRL">0.0000000000</LastPric>
                <FrstPric Ccy="BRL">0.0000000000</FrstPric>
                <DaysToSttlm>3</DaysToSttlm>
                <RghtsIssePric Ccy="BRL">0</RghtsIssePric>
                <AsstSubTp>ACAO</AsstSubTp>
                <AuctnTp>50</AuctnTp>
              </EqtyInf>
            </InstrmInf>
          </Instrm>
        </Document>
      </BizGrp>
      <BizGrp>
        <AppHdr xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
          <BizMsgIdr>BV000327201803190327106886775257713</BizMsgIdr>
          <MsgDefIdr>bvmf.100.02</MsgDefIdr>
          <CreDt>2018-03-19T21:18:06Z</CreDt>
          <Fr>
            <OrgId>
              <Id>
                <OrgId>
                  <Othr>
                    <Id>BVMF</Id>
                    <SchmeNm>
                      <Prtry>39</Prtry>
                    </SchmeNm>
                    <Issr>40</Issr>
                  </Othr>
                </OrgId>
              </Id>
            </OrgId>
          </Fr>
          <To>
            <OrgId>
              <Id>
                <OrgId>
                  <Othr>
                    <Id>PUBLIC</Id>
                    <SchmeNm>
                      <Prtry>39</Prtry>
                    </SchmeNm>
                    <Issr>40</Issr>
                  </Othr>
                </OrgId>
              </Id>
            </OrgId>
          </To>
        </AppHdr>
        <Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:bvmf.100.02.xsd">
          <Instrm>
            <RptParams>
              <ActvtyInd>true</ActvtyInd>
              <Frqcy>DAIL</Frqcy>
              <NetPosId>XXXX</NetPosId>
              <RptDtAndTm>
                <Dt>2018-03-19</Dt>
              </RptDtAndTm>
              <UpdTp>COMP</UpdTp>
            </RptParams>
            <FinInstrmId>
              <OthrId>
                <Id>100000099441</Id>
                <Tp>
                  <Prtry>8</Prtry>
                </Tp>
              </OthrId>
              <PlcOfListg>
                <MktIdrCd>BVMF</MktIdrCd>
              </PlcOfListg>
            </FinInstrmId>
            <FinInstrmAttrCmon>
              <Asst>BGI</Asst>
              <AsstDesc>Boi Gordo</AsstDesc>
              <Mkt>4</Mkt>
              <Sgmt>4</Sgmt>
              <Desc>BOI GORDO R$</Desc>
            </FinInstrmAttrCmon>
            <InstrmInf>
              <DrvsOptnExrcInf>
                <SctyCtgy>17</SctyCtgy>
                <TckrSymb>BGIZ18P013100E</TckrSymb>
                <ISIN>BRBMEFVBZEG5</ISIN>
                <OptnDlvryTp>0</OptnDlvryTp>
                <DerivOptnExrcInstrmId>
                  <OthrId>
                    <Id>100000099440</Id>
                    <Tp>
                      <Prtry>8</Prtry>
                    </Tp>
                  </OthrId>
                  <PlcOfListg>
                    <MktIdrCd>BVMF</MktIdrCd>
                  </PlcOfListg>
                </DerivOptnExrcInstrmId>
                <WdrwlDays>196</WdrwlDays>
                <WrkgDays>193</WrkgDays>
                <ClnrDays>284</ClnrDays>
              </DrvsOptnExrcInf>
            </InstrmInf>
          </Instrm>
        </Document>
      </BizGrp>
      <BizGrp>
        <AppHdr xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
          <BizMsgIdr>BV000327201803190327106886268001848</BizMsgIdr>
          <MsgDefIdr>bvmf.100.02</MsgDefIdr>
          <CreDt>2018-03-19T21:18:06Z</CreDt>
          <Fr>
            <OrgId>
              <Id>
                <OrgId>
                  <Othr>
                    <Id>BVMF</Id>
                    <SchmeNm>
                      <Prtry>39</Prtry>
                    </SchmeNm>
                    <Issr>40</Issr>
                  </Othr>
                </OrgId>
              </Id>
            </OrgId>
          </Fr>
          <To>
            <OrgId>
              <Id>
                <OrgId>
                  <Othr>
                    <Id>PUBLIC</Id>
                    <SchmeNm>
                      <Prtry>39</Prtry>
                    </SchmeNm>
                    <Issr>40</Issr>
                  </Othr>
                </OrgId>
              </Id>
            </OrgId>
          </To>
        </AppHdr>
        <Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:bvmf.100.02.xsd">
          <Instrm>
            <RptParams>
              <ActvtyInd>true</ActvtyInd>
              <Frqcy>DAIL</Frqcy>
              <NetPosId>XXXX</NetPosId>
              <RptDtAndTm>
                <Dt>2018-03-19</Dt>
              </RptDtAndTm>
              <UpdTp>COMP</UpdTp>
            </RptParams>
            <FinInstrmId>
              <OthrId>
                <Id>200000162829</Id>
                <Tp>
                  <Prtry>8</Prtry>
                </Tp>
              </OthrId>
              <PlcOfListg>
                <MktIdrCd>BVMF</MktIdrCd>
              </PlcOfListg>
            </FinInstrmId>
            <FinInstrmAttrCmon>
              <Asst>SMLS</Asst>
              <AsstDesc>SMLS</AsstDesc>
              <Mkt>12</Mkt>
              <Sgmt>1</Sgmt>
              <Desc>SMLS        ON   86,00</Desc>
            </FinInstrmAttrCmon>
            <InstrmInf>
              <ExrcEqtsInf>
                <SctyCtgy>17</SctyCtgy>
                <TckrSymb>SMLSC86E</TckrSymb>
                <ISIN>BRSMLS3C00L6</ISIN>
                <TradgCcy>BRL</TradgCcy>
                <TradgStartDt>2018-02-22</TradgStartDt>
                <TradgEndDt>2018-03-19</TradgEndDt>
                <DlvryTp>1</DlvryTp>
                <OptnExrcInstrmId>
                  <OthrId>
                    <Id>200000161407</Id>
                    <Tp>
                      <Prtry>8</Prtry>
                    </Tp>
                  </OthrId>
                  <PlcOfListg>
                    <MktIdrCd>BVMF</MktIdrCd>
                  </PlcOfListg>
                </OptnExrcInstrmId>
              </ExrcEqtsInf>
            </InstrmInf>
          </Instrm>
        </Document>
      </BizGrp>
      <BizGrp>
        <AppHdr xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
          <BizMsgIdr>BV000327201803190327106886362185834</BizMsgIdr>
          <MsgDefIdr>bvmf.100.02</MsgDefIdr>
          <CreDt>2018-03-19T21:18:06Z</CreDt>
          <Fr>
            <OrgId>
              <Id>
                <OrgId>
                  <Othr>
                    <Id>BVMF</Id>
                    <SchmeNm>
                      <Prtry>39</Prtry>
                    </SchmeNm>
                    <Issr>40</Issr>
                  </Othr>
                </OrgId>
              </Id>
            </OrgId>
          </Fr>
          <To>
            <OrgId>
              <Id>
                <OrgId>
                  <Othr>
                    <Id>PUBLIC</Id>
                    <SchmeNm>
                      <Prtry>39</Prtry>
                    </SchmeNm>
                    <Issr>40</Issr>
                  </Othr>
                </OrgId>
              </Id>
            </OrgId>
          </To>
        </AppHdr>
        <Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:bvmf.100.02.xsd">
          <Instrm>
            <RptParams>
              <ActvtyInd>true</ActvtyInd>
              <Frqcy>DAIL</Frqcy>
              <NetPosId>XXXX</NetPosId>
              <RptDtAndTm>
                <Dt>2018-03-19</Dt>
              </RptDtAndTm>
              <UpdTp>COMP</UpdTp>
            </RptParams>
            <FinInstrmId>
              <OthrId>
                <Id>100000099440</Id>
                <Tp>
                  <Prtry>8</Prtry>
                </Tp>
              </OthrId>
              <PlcOfListg>
                <MktIdrCd>BVMF</MktIdrCd>
              </PlcOfListg>
            </FinInstrmId>
            <FinInstrmAttrCmon>
              <Asst>BGI</Asst>
              <AsstDesc>Boi Gordo</AsstDesc>
              <Mkt>4</Mkt>
              <Sgmt>4</Sgmt>
              <Desc>BOI GORDO R$</Desc>
            </FinInstrmAttrCmon>
            <InstrmInf>
              <OptnOnSpotAndFutrsInf>
                <ISIN>BRBMEFVBZEG5</ISIN>
                <TckrSymb>BGIZ18P013100</TckrSymb>
                <ExrcPric Ccy="BRL">131</ExrcPric>
                <ExrcStyle>AMER</ExrcStyle>
                <XprtnDt>2018-12-28</XprtnDt>
                <XprtnCd>ZLM1</XprtnCd>
                <OptnTp>PUTT</OptnTp>
                <CtrctMltplr>330.000000000</CtrctMltplr>
                <AsstQtnQty>1.000000000</AsstQtnQty>
                <PmtTp>0</PmtTp>
                <AllcnRndLot>1</AllcnRndLot>
                <CFICd>OPATPS</CFICd>
                <UndrlygInstrmId>
                  <OthrId>
                    <Id>100000097542</Id>
                    <Tp>
                      <Prtry>8</Prtry>
                    </Tp>
                  </OthrId>
                  <PlcOfListg>
                    <MktIdrCd>BVMF</MktIdrCd>
                  </PlcOfListg>
                </UndrlygInstrmId>
                <PrmUpfrntInd>true</PrmUpfrntInd>
                <TradgStartDt>2018-02-05</TradgStartDt>
                <TradgEndDt>2018-12-28</TradgEndDt>
                <OpngPosLmtDt>2018-12-27</OpngPosLmtDt>
                <TradgCcy>BRL</TradgCcy>
                <WdrwlDays>196</WdrwlDays>
                <WrkgDays>193</WrkgDays>
                <ClnrDays>284</ClnrDays>
              </OptnOnSpotAndFutrsInf>
            </InstrmInf>
          </Instrm>
        </Document>
      </BizGrp>
     </Xchg>
  </BizFileHdr>
 </Document>

BVMF
40
39
公开的
40
39
BV000327201803190327120433334899343
38302
BVBG.028.02
2018-03-19T18:18:20
bvmf.100.02
38302
BV000327201803190327106886457976417
bvmf.100.02
2018-03-19T21:18:06Z
BVMF
39
40
公开的
39
40
真的
达尔
XXXX
2018-03-19
公司
200000162828
8.
BVMF
HGLG
HGLG
17
1.
FII CSHG LOGCI
25
BRHGLGCTF004
190
西西鲁
词
CSHG LOGÍSTICA FDO INV IMOB-FII
HGLG11L
1.
1.
1.
9999-12-31
9999-12-31
9999-12-31
0
0
BRL
671978
0
0
3.
0
阿考
50
BV000327201803190327106886775257713
bvmf.100.02
2018-03-19T21:18:06Z
BVMF
39
40
公开的
39
40
真的
达尔
XXXX
2018-03-19
公司
100000099441
8.
BVMF
BGI
博伊戈多
4.
4.
博伊戈多河$
17
BGIZ18P013100E
BRBMEFVBZEG5
0
100000099440
8.
BVMF
196
193
284
BV000327201803190327106886268001848
bvmf.100.02
2018-03-19T21:18:06Z
BVMF
39
40
公开的
39
40
真的
达尔
XXXX
2018-03-19
公司
200000162829
8.
BVMF
SMLS
SMLS
12
1.
86,00上的SMLS
17
SMLSC86E
BRSMLS3C00L6
BRL
2018-02-22
2018-03-19
1.
200000161407
8.
BVMF
import xml.etree.ElementTree as ET

tree = ET.parse('file.xml')
root = tree.getroot()
ns = {'ns': 'urn:bvmf.100.02.xsd'}    # namespace mapping 
el = root.find('.//ns:OptnOnSpotAndFutrsInf', ns)
tckr = el.findtext('ns:TckrSymb', None, ns)
cficd = el.findtext('ns:CFICd', None, ns)

print(tckr, cficd)
BGIZ18P013100 OPATPS