我正在使用lxml lib来解析XML(我的XML文件使用XML签名)到python对象,除了一个案例之外一切正常,Signature
标签下面的xml模型没有被解析为对象
from lxml import etree,objectify
xml_object = objectify.fromstring(xml_string)
# This works
xml_object.infNFe
#This doesn't works, raise the exception that Signature attribute is not defined
xml_object.Signature
PyCharm调试,Signature tag
未解析
好奇心,如果我尝试解析它只有Signature
标签可行,但如果它与其他标签一起使用,它就不起作用。
我看过关于这个问题的网站,但我找不到任何解决方案!
那么,问题是,如何解析下面的整个XML?
使用的XML
<?xml version="1.0" encoding="UTF-8"?>
<NFe>
<infNFe Id="NFe24151011111111111111550010000000301973817012" versao="3.10">
<ide>
<cUF>24</cUF>
<cNF>97381701</cNF>
<natOp>Venda de produto do estabelecimento</natOp>
<indPag>0</indPag>
<mod>55</mod>
<serie>1</serie>
<nNF>30</nNF>
<dhEmi>2015-10-08T15:38:45-03:00</dhEmi>
<dhSaiEnt>2015-10-08T15:38:45-03:00</dhSaiEnt>
<tpNF>1</tpNF>
<idDest>1</idDest>
<cMunFG>2408003</cMunFG>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
<cDV>2</cDV>
<tpAmb>2</tpAmb>
<finNFe>1</finNFe>
<indFinal>0</indFinal>
<indPres>1</indPres>
<procEmi>0</procEmi>
<verProc>PyComp Modulo NFe</verProc>
</ide>
<emit>
<CNPJ>11111111111111</CNPJ>
<xNome>Razão Social Ltda. EPP</xNome>
<xFant>Nome Fantasia</xFant>
<enderEmit>
<xLgr>Al. Kenworthy</xLgr>
<nro>140</nro>
<xBairro>Jd. Santa Rosália</xBairro>
<cMun>2408003</cMun>
<xMun>Mossoro</xMun>
<UF>RN</UF>
<CEP>59600000</CEP>
<cPais>1058</cPais>
<fone>1534110602</fone>
</enderEmit>
<IE>111111111111</IE>
<CRT>1</CRT>
</emit>
<dest>
<CNPJ>11111111111111</CNPJ>
<xNome>EMPRESA EXEMPLO LTDA</xNome>
<enderDest>
<xLgr>RUA NISIA FLORESTA</xLgr>
<nro>846</nro>
<xBairro>BOA VISTA</xBairro>
<cMun>2408003</cMun>
<xMun>MOSSORO</xMun>
<UF>RN</UF>
<cPais>1058</cPais>
<xPais>BRASIL</xPais>
<fone>1534110602</fone>
</enderDest>
<indIEDest>9</indIEDest>
<IE>111111111111</IE>
</dest>
<det nItem="1">
<prod>
<cProd>1253236</cProd>
<cEAN>7896297837046</cEAN>
<xProd>Descrição do produto</xProd>
<NCM>999999999</NCM>
<CFOP>5102</CFOP>
<uCom>UN</uCom>
<qCom>100.00</qCom>
<vUnCom>10.00</vUnCom>
<vProd>1000.00</vProd>
<cEANTrib>7896297837046</cEANTrib>
<uTrib>UN</uTrib>
<qTrib>100.00</qTrib>
<vUnTrib>10.00</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
<vTotTrib>18.00</vTotTrib>
<ICMS>
<ICMS00>
<orig>0</orig>
<CST>00</CST>
<modBC>3</modBC>
<vBC>1.00</vBC>
<pICMS>17.00</pICMS>
<vICMS>0.17</vICMS>
</ICMS00>
</ICMS>
</imposto>
</det>
<det nItem="2">
<prod>
<cProd>1253236</cProd>
<cEAN>7896297837046</cEAN>
<xProd>PRODUTO 2</xProd>
<NCM>999999999</NCM>
<CFOP>5102</CFOP>
<uCom>UN</uCom>
<qCom>1.00</qCom>
<vUnCom>1.00</vUnCom>
<vProd>1.00</vProd>
<cEANTrib>7896297837046</cEANTrib>
<uTrib>UN</uTrib>
<qTrib>1.00</qTrib>
<vUnTrib>1.00</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
<vTotTrib>18.17</vTotTrib>
<ICMS>
<ICMS00>
<orig>0</orig>
<CST>00</CST>
<modBC>3</modBC>
<vBC>1.00</vBC>
<pICMS>17.00</pICMS>
<vICMS>0.17</vICMS>
</ICMS00>
</ICMS>
</imposto>
</det>
<total>
<ICMSTot>
<vBC>1000.00</vBC>
<vICMS>180.00</vICMS>
<vICMSDeson>0.00</vICMSDeson>
<vBCST>0.00</vBCST>
<vST>0.00</vST>
<vProd>1000.00</vProd>
<vFrete>0.00</vFrete>
<vSeg>0.00</vSeg>
<vDesc>0.00</vDesc>
<vII>0.00</vII>
<vIPI>0.00</vIPI>
<vPIS>0.00</vPIS>
<vCOFINS>0.00</vCOFINS>
<vOutro>0.00</vOutro>
<vNF>1100.00</vNF>
<vTotTrib>0.00</vTotTrib>
</ICMSTot>
</total>
<transp>
<modFrete>9</modFrete>
</transp>
</infNFe>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#NFe24151011111111111111550010000000301973817012">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>OWcT6jHwBr4c3kGNBwzFoQ03oHQ=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>BxP66jlYeNfD2JPKkUaih3gJzMvxIWWZsGDqps/lylpW55uyCl4DnK9ppdXAzKNsoRwyQz5fPE8CECSbJbkZkIth82pK0Ul7ZjWsogD6luncJ78oS+B6xFqUL3ypm5mtq+vkQOK8ljdBxAh8gYRW/V8yEMu2rTcmI1vki6PGOMnItM68e7vpJl01GLI2FwI9gLBDNjHJJ2i8snJL6waS/PHx+UOX2vYogIGbtw+y11FI0brcm056AjyIycZsEg8XByoVB7WfLVNIpCPwdDrDjMV6TOTX9MGMuoVRxAYz+7W5PEkJTU6Sd5FmHcsRkw7SeDaXGgdcBR7SIbrnOOH6zQ==</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>MIIIcjCCBlqgAwIBAgIQNRo7p1A4dwNd7Yu7TSyJ7jANBgkqhkiG9w0BAQsFADB4MQswCQYDVQQGEwJCUjETMBEGA1UEChM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a6Ufz166TdPhSj1M3t7s9Mwq5P4XE014j7uRB0k4iaiNrVCBY7M3RlFEpK55cwpjSQdZUxv0+2C5yUeoIksaW05lcofXSfJTE6x/eGk/Qnrs/VzcFdhiUUOX1cpd9xff++z1nsISwDqpQHWkjSVeLSG7e51mB51bp1XbDyd2/vp/xLHIPz5syHo7H5fp4ibDN+WNRWaGCeaGzgKZjyg2PXt3WHZY+BCQ25hRcTi234HxuEdZNATSDLdsdGiSd55YyPcZIK8FN8/ahJlUnP689jnEhQAt2lj/JuGD96efWlltMTIB38UCviJEhVcazBW7Frg8YBTtdLIclcNoYNjVBbiGtC/xMS53PwQJ6q3yApth4BiDkS4ZFkk4DqaQ3hPExFpfjZ4fDLEt8MT5SBTD/3Efn7CMy0rx2Bq4H1mWEzJ2SCU01iMcqGPdg5TR7OzraCnztjfAK6DFlO3Q9EzrM5Rn5LtmPpRlpTRrr0HzH+movukshOqZO9yXgKkSuf6RD3rKGgNP+O5idXegi9frr+Hp9TA8eejf7IpvYSAEvDJHs8uIKvmbhcGLNpSL+mWod/H23j9sUFkmqHOahxhhOqg9LzShdEbtlts+XaeZTlGz2D3MKG/LjPkDw+QkrWIBTS8zOfrj0MeMtuW/uDKoLL6iQzYj</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</NFe>
答案 0 :(得分:0)
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
xmlns
属性存在问题。 lmlx
会尝试将其解析为namespase。只需将其删除或更换即可。
>>> print etree.tostring(xml_object.Signature, pretty_print=True)
<Signature>
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#NFe24151011111111111111550010000000301973817012">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>OWcT6jHwBr4c3kGNBwzFoQ03oHQ=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>...</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>....</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>