从组标签获取字符串文本

时间:2019-04-30 13:49:21

标签: python xml lxml elementtree

我想要获取组标签xml的内容字符串

以这个xml为例:

// optional positional parameters
location([num lat = 20, num lat = 20]) {}
// optional named parameters
location({num lat = 20, num lat = 20}) {}

我想获取infEvento组的字符串内容。像这样:

<retEvento versao="1.00" xmlns="http://www.portalfiscal.inf.br/nfe">
    <infEvento>
        <tpAmb>2</tpAmb>
        <verAplic>SVRSnfce201808211044</verAplic>
        <cOrgao>22</cOrgao>
        <cStat>135</cStat>
        <xMotivo>Evento registrado e vinculado a NF-e</xMotivo>
        <chNFe>XXXXXXXXXXXXXXXXXXXXXXXXXXX</chNFe>
        <tpEvento>110111</tpEvento>
        <nSeqEvento>1</nSeqEvento>
        <dhRegEvento>2019-04-17T15:59:45-03:00</dhRegEvento>
        <nProt>YYYYYYYYYYYYYYY</nProt>
    </infEvento>
</retEvento>

我尝试:

<infEvento>
        <tpAmb>2</tpAmb>
        <verAplic>SVRSnfce201808211044</verAplic>
        <cOrgao>22</cOrgao>
        <cStat>135</cStat>
        <xMotivo>Evento registrado e vinculado a NF-e</xMotivo>
        <chNFe>XXXXXXXXXXXXXXXXXXXXXXXXXXX</chNFe>
        <tpEvento>110111</tpEvento>
        <nSeqEvento>1</nSeqEvento>
        <dhRegEvento>2019-04-17T15:59:45-03:00</dhRegEvento>
        <nProt>YYYYYYYYYYYYYYY</nProt>
    </infEvento>

但是我什么也没得到:(

1 个答案:

答案 0 :(得分:2)

尝试使用tostring方法。

etree.tostring(el.xpath('//ns:retEvento',namespaces=ns)[0])