Reading of Java XML

Asked

Viewed 1,216 times

0

I’m trying to read an XML in Java to get data from an NF-e.

I tried so to try to get only the product code to test more unsuccessfully:

 public void testeaa(String xml) throws DocumentException {

    Document document = getDocumento(xml);
    Element root = document.getRootElement();

    Iterator<Element> i = root.elementIterator();

    i.forEachRemaining(element -> {

        if (element.getQualifiedName().equals("det nItem")) {
            System.out.println("COdProd: " + element.getText());
        }


    });
}

public Document getDocumento(String xml) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(xml);
    return document;
}

XML example:

<?xml version="1.0" encoding="UTF-8"?>
<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="3.10">
   <NFe>
      <infNFe Id="NFe41180710930674000149550010000005561136042494" versao="3.10">
         <ide>
            <cUF>41</cUF>
            <cNF>13604249</cNF>
            <natOp>Venda</natOp>
            <indPag>0</indPag>
            <mod>55</mod>
            <serie>1</serie>
            <nNF>556</nNF>
            <dhEmi>2018-07-31T15:04:00-03:00</dhEmi>
            <dhSaiEnt>2018-07-31T15:04:00-03:00</dhSaiEnt>
            <tpNF>1</tpNF>
            <idDest>1</idDest>
            <cMunFG>4115002</cMunFG>
            <tpImp>1</tpImp>
            <tpEmis>1</tpEmis>
            <cDV>4</cDV>
            <tpAmb>1</tpAmb>
            <finNFe>1</finNFe>
            <indFinal>1</indFinal>
            <indPres>1</indPres>
            <procEmi>0</procEmi>
            <verProc>0.0.1</verProc>
         </ide>
         <emit>
            <CNPJ>10930674000149</CNPJ>
            <xNome>COMERCIO DE PRODUTOS AGROPECUARIOS JSM EIRELI</xNome>
            <xFant>CASA DO AGRICULTOR</xFant>
            <enderEmit>
               <xLgr>AV PARANA</xLgr>
               <nro>996</nro>
               <xBairro>CENTRO</xBairro>
               <cMun>4115002</cMun>
               <xMun>MARILENA</xMun>
               <UF>PR</UF>
               <CEP>87960000</CEP>
               <cPais>1058</cPais>
               <xPais>BRASIL</xPais>
               <fone>4434481934</fone>
            </enderEmit>
            <IE>9048560455</IE>
            <CRT>1</CRT>
         </emit>
         <dest>
            <CPF>82726647987</CPF>
            <xNome>LUIS CARLOS BONOME</xNome>
            <enderDest>
               <xLgr>SITIO SAO PAULO</xLgr>
               <nro>S/N</nro>
               <xBairro>SEIS CASAS</xBairro>
               <cMun>4111308</cMun>
               <xMun>ITAUNA DO SUL</xMun>
               <UF>PR</UF>
               <CEP>87980000</CEP>
               <cPais>1058</cPais>
               <xPais>BRASIL</xPais>
               <fone>4488326605</fone>
            </enderDest>
            <indIEDest>9</indIEDest>
         </dest>
         <det nItem="1">
            <prod>
               <cProd>5409</cProd>
               <cEAN />
               <xProd>BRUCELINA B-19 30 ML (15 DOSES)</xProd>
               <NCM>30023080</NCM>
               <CFOP>5102</CFOP>
               <uCom>uni</uCom>
               <qCom>1.0000</qCom>
               <vUnCom>18.200000</vUnCom>
               <vProd>18.20</vProd>
               <cEANTrib />
               <uTrib>uni</uTrib>
               <qTrib>1.0000</qTrib>
               <vUnTrib>18.200000</vUnTrib>
               <indTot>1</indTot>
            </prod>
            <imposto>
               <vTotTrib>0.76</vTotTrib>
               <ICMS>
                  <ICMSSN102>
                     <orig>0</orig>
                     <CSOSN>103</CSOSN>
                  </ICMSSN102>
               </ICMS>
               <IPI>
                  <cEnq>301</cEnq>
                  <IPINT>
                     <CST>52</CST>
                  </IPINT>
               </IPI>
               <PIS>
                  <PISNT>
                     <CST>07</CST>
                  </PISNT>
               </PIS>
               <COFINS>
                  <COFINSNT>
                     <CST>07</CST>
                  </COFINSNT>
               </COFINS>
            </imposto>
         </det>
         <det nItem="2">
            <prod>
               <cProd>3997</cProd>
               <cEAN />
               <xProd>BRUSKY ADULTO  15KG</xProd>
               <NCM>23091000</NCM>
               <CFOP>5102</CFOP>
               <uCom>uni</uCom>
               <qCom>1.0000</qCom>
               <vUnCom>39.900000</vUnCom>
               <vProd>39.90</vProd>
               <cEANTrib />
               <uTrib>uni</uTrib>
               <qTrib>1.0000</qTrib>
               <vUnTrib>39.900000</vUnTrib>
               <indTot>1</indTot>
            </prod>
            <imposto>
               <vTotTrib>3.63</vTotTrib>
               <ICMS>
                  <ICMSSN102>
                     <orig>0</orig>
                     <CSOSN>103</CSOSN>
                  </ICMSSN102>
               </ICMS>
               <IPI>
                  <cEnq>301</cEnq>
                  <IPINT>
                     <CST>52</CST>
                  </IPINT>
               </IPI>
               <PIS>
                  <PISNT>
                     <CST>07</CST>
                  </PISNT>
               </PIS>
               <COFINS>
                  <COFINSNT>
                     <CST>07</CST>
                  </COFINSNT>
               </COFINS>
            </imposto>
         </det>
         <det nItem="3">
            <prod>
               <cProd>4544</cProd>
               <cEAN />
               <xProd>BAYTICOL POUR-ON 1L</xProd>
               <NCM>38089999</NCM>
               <CFOP>5102</CFOP>
               <uCom>uni</uCom>
               <qCom>1.0000</qCom>
               <vUnCom>98.000000</vUnCom>
               <vProd>98.00</vProd>
               <cEANTrib />
               <uTrib>uni</uTrib>
               <qTrib>1.0000</qTrib>
               <vUnTrib>98.000000</vUnTrib>
               <indTot>1</indTot>
            </prod>
            <imposto>
               <vTotTrib>4.12</vTotTrib>
               <ICMS>
                  <ICMSSN102>
                     <orig>0</orig>
                     <CSOSN>103</CSOSN>
                  </ICMSSN102>
               </ICMS>
               <IPI>
                  <cEnq>301</cEnq>
                  <IPINT>
                     <CST>52</CST>
                  </IPINT>
               </IPI>
               <PIS>
                  <PISNT>
                     <CST>07</CST>
                  </PISNT>
               </PIS>
               <COFINS>
                  <COFINSNT>
                     <CST>07</CST>
                  </COFINSNT>
               </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>156.10</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>156.10</vNF>
               <vTotTrib>8.51</vTotTrib>
            </ICMSTot>
         </total>
         <transp>
            <modFrete>1</modFrete>
         </transp>
         <cobr>
            <dup>
               <nDup>1</nDup>
               <dVenc>2018-08-24</dVenc>
               <vDup>156.10</vDup>
            </dup>
         </cobr>
      </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="#NFe41180710930674000149550010000005561136042494">
               <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>eBgQo1KZj/0m4GrTiqMZ3KPmM1k=</DigestValue>
            </Reference>
         </SignedInfo>
         <SignatureValue>XEPqu9honydrDj0LYZFnJbhGAx4CIhxZPyNh/9iyZzp4UD3zPP3oSh6y6tj9hD9Hltitv3tpK3VqtUgUddfTY2GFKQ9q0xrAm82bGhNzk2tGDed1HIMOb7uLaIOdb59oFkwnmoG4q4XgWuD1h8JZ1wscYo4gyBvUPyHV0QmwscszGivGYamqh0opEgpDaB8Gk/MyO1GWODCdmnETIGZULxS3XYw1PNZAnKfOCW8d6WkY3l8owQNR1WipA0HbTH95DBU0HYjl9/h/6fyvUc4ekEoWiUkAaVbm+XNIdjkbY+9nQAOoNlA7GKzava9J9cHPVuikFoAv+rjP6EGPZYua3g==</SignatureValue>
         <KeyInfo>
            <X509Data>
               <X509Certificate>MIIIEDCCBfigAwIBAgIQF58IxhG5SzlAAG/hGgt0lzANBgkqhkiG9w0BAQsFADB4MQswCQYDVQQGEwJCUjETMBEGA1UEChMKSUNQLUJyYXNpbDE2MDQGA1UECx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eiw7pYc3niBc6Jn2Yc1WnSlsoT1/+UzLbHvE+WMSEzvUcgm028+LfqcYf+KKc/YJfMPG29y2/LWfRYp6qHaLj0Solgrfh5gvyo0e6kxLX9JCc4o0XBmiPT51awolkcvnDkR7F1D87iQo4g7F4m43VO5dqBqePw/syMQrWFvndlwHlWujECy2QuE/2j9Xrgn5JKwViGVSvaf50a+KvT98s/KZuQYk//gXzzlQ1GqwzhQPMYt7SUXpQ4Du2bBTUSvbMxI42iN/Py/jgd26U/BKVPgtP7o3us8K71iiMh6IOp/AtCMMnMcZ5nm+XoQI8ciuMykd6dTMBBKbg8U3sZ1rXxX8hxLw/ux0BhnT5aWM5/zYgkUFz4vvDMgFsSoswSMARgUuS+C7K+2Y4RgQCxxtk1S8ecGbOD0OrExgv1StEChKx/PjhtqWkoKQY1y/302COeozJAhmeD1oOnZDyrIAtbW6Qz2sUYh2fEuH6LDuLRfAwFyIP0PwRHkdVO7KC7qZTUNSIaRRw==</X509Certificate>
            </X509Data>
         </KeyInfo>
      </Signature>
   </NFe>
   <protNFe versao="3.10">
      <infProt>
         <tpAmb>1</tpAmb>
         <verAplic>PR-v3_9_5</verAplic>
         <chNFe>41180710930674000149550010000005561136042494</chNFe>
         <dhRecbto>2018-07-31T15:04:04-03:00</dhRecbto>
         <nProt>141180128267723</nProt>
         <digVal>eBgQo1KZj/0m4GrTiqMZ3KPmM1k=</digVal>
         <cStat>100</cStat>
         <xMotivo>Autorizado o uso da NF-e</xMotivo>
      </infProt>
   </protNFe>
</nfeProc>

Would you have any idea?

  • You would have an example XML so that other people can test your code and the modifications you want to make to it?

  • I inserted in the question, it is an xml of a nfe, accurate of the data and go through the products.

1 answer

1


First, the method of SAXReader.read(String) that you’re using on getDocumento expects a URL or a file name (https://dom4j.github.io/javadoc/2.0.1/org/dom4j/io/SAXReader.html#read-java.lang.String-), so an error will occur if you pass XML directly (at least that’s what happened when trying to run your code). To fix this, you can use the method or method SAXReader.read(Reader) passing a StringReader containing the XML.

Example of how it would look:

public static Document getDocumento(String xml) throws DocumentException {
  SAXReader reader = new SAXReader();
  Document document = reader.read(new StringReader(xml));
  return document;
}

Now, to get the product code:

Assuming the product code is what is inside the tag <cProd> (I’m not sure, because I don’t know the structure of an NFE), you can use the following code:

Document document = getDocumento(xml);
Element root = document.getRootElement();

root.accept(new VisitorSupport() {
  @Override
  public void visit(Element node) {
    if (node.getQualifiedName().equals("cProd")) {
      System.out.println(node.getText());
    }
  }
});

And this will show:

5409
3997
4544

You can also catch the <det>, with this you can access each child element separately: (Useful in case you need to access not only the cProd)

root.accept(new VisitorSupport() {
  @Override
  public void visit(Element node) {
    if (node.getQualifiedName().equals("det")) {
      String attributoNItem = node.attributeValue("nItem");

      System.out.println("Item " + attributoNItem);

      Element prodNode = (Element) node.element("prod");
      System.out.println("Código: " + prodNode.element("cProd").getText());
    }
  }
});

Exit:

Item 1
Código: 5409
Item 2
Código: 3997
Item 3
Código: 4544
  • Exactly what I needed, thank you :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.