Java jDom2 获取标签值 DigestValue

Java jDom2 get Tag Value DigestValue

我正在使用 XMLQuire 生成补丁以获取补丁。 但总是 return 空值:

使用的 Xpath:

//dft:enviNFe/dft:NFe/Signature/SignedInfo/Reference/DigestValue

那个有效:

//k:enviNFe/k:NFe/k:infNFe/k:ide/k:tpAmb

使用的函数:

XPath xPath = XPath.newInstance(SignatureValue);
xPath.addNamespace("dft", d.getRootElement().getNamespaceURI());
Element node = (Element) xPath.selectSingleNode(d.getRootElement());

XML:

<?xml version="1.0" encoding="UTF-8"?>
<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="3.10">
  <idLote>1</idLote>
  <indSinc>1</indSinc>
  <NFe xmlns="http://www.portalfiscal.inf.br/nfe">
    <infNFe Id="NFe411511108929620001566500100000000713243" versao="3.10">
      <ide>
        <cUF>41</cUF>
        <cNF>63872751</cNF>
        <natOp>Venda</natOp>
        <indPag>0</indPag>
        <mod>65</mod>
        <serie>1</serie>
        <nNF>7</nNF>
        <dhEmi>2015-11-09T08:37:35-02:00</dhEmi>
        <tpNF>1</tpNF>
        <idDest>1</idDest>
        <cMunFG>4108304</cMunFG>
        <tpImp>4</tpImp>
        <tpEmis>1</tpEmis>
        <cDV>3</cDV>
        <tpAmb>2</tpAmb>
        <finNFe>1</finNFe>
        <indFinal>1</indFinal>
        <indPres>1</indPres>
        <procEmi>0</procEmi>
        <verProc>3.0</verProc>
      </ide>
      <emit>
        <CNPJ>108XXXX2000156</CNPJ>
        <xNome>X XXX XXX</xNome>
        <xFant>X XXX XXX</xFant>
        <enderEmit>
          <xLgr>XXXXX XXX</xLgr>
          <nro>XXXX</nro>
          <xBairro>Vila Residencial</xBairro>
          <cMun>XXXXX</cMun>
          <xMun>Foz do Iguacu</xMun>
          <UF>PR</UF>
          <CEP>XXXXXX</CEP>
          <cPais>XXXX</cPais>
          <xPais>Brasil</xPais>
          <fone>XXX XXX</fone>
        </enderEmit>
        <IE>90XXXXX48x5x80</IE>
        <CRT>1</CRT>
      </emit>
      <dest>
        <CNPJ>99999999000191</CNPJ>
        <xNome>NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL</xNome>
        <enderDest>
          <xLgr>Travessa Kayaby</xLgr>
          <nro>400</nro>
          <xBairro>Jardim Renato Festugato</xBairro>
          <cMun>4108304</cMun>
          <xMun>Foz do Iguacu</xMun>
          <UF>PR</UF>
          <CEP>85864110</CEP>
          <cPais>1058</cPais>
          <xPais>Brasil</xPais>
          <fone>4599999999</fone>
        </enderDest>
        <indIEDest>9</indIEDest>
      </dest>
      <det nItem="1">
        <prod>
          <cProd>21753</cProd>
          <cEAN/>
          <xProd>NOTA FISCAL EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL</xProd>
          <NCM>84099116</NCM>
          <CFOP>5102</CFOP>
          <uCom>UN</uCom>
          <qCom>3.0</qCom>
          <vUnCom>1.56</vUnCom>
          <vProd>4.68</vProd>
          <cEANTrib/>
          <uTrib>UND</uTrib>
          <qTrib>3.0</qTrib>
          <vUnTrib>1.56</vUnTrib>
          <indTot>1</indTot>
        </prod>
        <imposto>
          <ICMS>
            <ICMSSN102>
              <orig>0</orig>
              <CSOSN>102</CSOSN>
            </ICMSSN102>
          </ICMS>
          <PIS>
            <PISOutr>
              <CST>99</CST>
              <vBC>0.00</vBC>
              <pPIS>0.00</pPIS>
              <vPIS>0.00</vPIS>
            </PISOutr>
          </PIS>
          <COFINS>
            <COFINSOutr>
              <CST>99</CST>
              <vBC>0.00</vBC>
              <pCOFINS>0.00</pCOFINS>
              <vCOFINS>0.00</vCOFINS>
            </COFINSOutr>
          </COFINS>
        </imposto>
      </det>
      <det nItem="2">
        <prod>
          <cProd>21753</cProd>
          <cEAN/>
          <xProd>NOTA FISCAL EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL</xProd>
          <NCM>84099116</NCM>
          <CFOP>5102</CFOP>
          <uCom>UN</uCom>
          <qCom>3.0</qCom>
          <vUnCom>1.56</vUnCom>
          <vProd>4.68</vProd>
          <cEANTrib/>
          <uTrib>UND</uTrib>
          <qTrib>3.0</qTrib>
          <vUnTrib>1.56</vUnTrib>
          <indTot>1</indTot>
        </prod>
        <imposto>
          <ICMS>
            <ICMSSN102>
              <orig>0</orig>
              <CSOSN>102</CSOSN>
            </ICMSSN102>
          </ICMS>
          <PIS>
            <PISOutr>
              <CST>99</CST>
              <vBC>0.00</vBC>
              <pPIS>0.00</pPIS>
              <vPIS>0.00</vPIS>
            </PISOutr>
          </PIS>
          <COFINS>
            <COFINSOutr>
              <CST>99</CST>
              <vBC>0.00</vBC>
              <pCOFINS>0.00</pCOFINS>
              <vCOFINS>0.00</vCOFINS>
            </COFINSOutr>
          </COFINS>
        </imposto>
      </det>
      <total>
        <ICMSTot>
          <vBC>0.00</vBC>
          <vICMS>0.00</vICMS>
          <vICMSDeson>0.00</vICMSDeson>
          <vBCST>0.00</vBCST>
          <vST>0.00</vST>
          <vProd>9.36</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>9.36</vNF>
        </ICMSTot>
      </total>
      <transp>
        <modFrete>9</modFrete>
        <vol>
          <qVol>0</qVol>
          <esp>-</esp>
          <marca>-</marca>
          <nVol>0</nVol>
          <pesoL>0</pesoL>
          <pesoB>0</pesoB>
        </vol>
      </transp>
      <pag>
        <tPag>01</tPag>
        <vPag>9.36</vPag>
      </pag>
      <infAdic>
        <infCpl>Venda de Mercadoria</infCpl>
      </infAdic>
    </infNFe>
    <infNFeSupl>
      <qrCode/>
    </infNFeSupl>
    <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="#NFe41151110892962000156650010000000071638727513">
          <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>mXo6QeJrfJKTp4Jm2W+MRCI6fJI=</DigestValue>
        </Reference>
      </SignedInfo>
      <SignatureValue>Iemd18kXXXXtUyUvD896XEuE2hNiN8tAaeDv54aoXXXXwbMOruPm1JMie5h3K4EAk3BXcRatNycLdhOvXHJU2WVMbUaZsm5UBt3UKDWxj9vR14Ure3tpKL85cSvORXwMrqBG7iX/kTV6wumTZdjYIXc7qTPV14krZTAu7JUKzZ9qkbvjUvaX5A6NGavfK+aSqPL9YPTvSACUEaqgvaOfO/TXAh7t3EkDOshwb9LI1DhbRzUCGG0dLV0m0f/dLA8XKXe2Wa0RvGmNxeO4KqAk1Q9zR+0nHjpaGcGUiOvVhLeJ9ZtzbogoaPZbmUJyMbM4x9aPTQT8bmA==</SignatureValue>
      <KeyInfo>
        <X509Data>
          <X509Certificate>MIIIXXXXXXAxrXtOdw1vDkMIjANBgkqhkiG9w0BAQsFADCBgDELMAkGA1UEBhMCQlIxEzARBgNVBAoTCklDUC1CcmFzaWwxNjA0BgNVBAsTLVNlY3JldGFyaWEgZGEgUXXZWl0YSBGZWRlcmFsIGRvIEJyYXNpbCAtIFJGQjEkMCIGA1UEAxMbQUMgSW5zdGl0dXRvIEZlbmFjb24gUkZCIEcyMB4XDTE1MDkyNDAwMDAwMFoXDTE2MDkyMjIzNTk1OVowgfcxCzAJBgNVBAYTAkJSMRMwEQYDVQQKFApJQ1AtQnJhc2lsMQswCQYDVQQIEwJQUjEWMBQGA1UEBxQNRk9aIERPIElHVUFDVTE2MDQGA1UECxQtU2VjcmV0YXJpYSBkYSBSZWNlaXRhIEZlZGVyYWwgZG8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JbI/czaLP8e8b4LEz06MjpWpoBpBwo/Os+ZGZ6EoTADJHlvZM2OaDmc3nm8RXIr4lPPaVP7CgvbAeyHoE2eZ76pN0A81WG4+Oh3x3NfpXywQaQqLCiOozEtVe70bsVJXI4JMl5RHb6AZPhdZ0zv5kZRvgP53vALv1t2VQLDaotYobKXQi+jUSlc+wwV6vNjcFeajPGruRdsmpviEJwlNJTLM/PdEMdgqS7mDGkvOjBZjPkt7NU6OCpaSiJ4jUzE/eP8Q2lI2t97pJyi0XF+hK/LPANruAOHnmnKjcljyX7tm2t5QQwSVxk7KrNXuNzB9b/iaKs+0eGCoQ==</X509Certificate>
        </X509Data>
      </KeyInfo>
    </Signature>
  </NFe>
</enviNFe>

根元素在命名空间 xmlns="http://www.portalfiscal.inf.br/nfe" 中,它的大部分子元素也在。

Signature 元素位于命名空间 mlns="http://www.w3.org/2000/09/xmldsig#" 及其所有子元素中。

因此,您的 XPath 中需要 2 个命名空间...

并且...您必须使用 JDOM-2 的新 XPath API,而不是旧的。所以,做:

Namespace sig = Namespace.getNamespace("sig", "http://www.w3.org/2000/09/xmldsig#");
Namespace nfe = Namespace.getNamespace("nfe", "http://www.portalfiscal.inf.br/nfe");

XPathFactory xfac = XPathFactory.instance();
XPathExpression<Element> digest = xfac.compile(
    "/nfe:enviNFe/nfe:NFe/sig:Signature/sig:SignedInfo/sig:Reference/sig:DigestValue",
    Filters.element(), null, sig, nfe);

String digestValue = digest.evaluateFirst().value();

documentation for compile.

而且,当您 post 提问时,请让您的 XML 更易于阅读。