Extract the products from an NFe XML

0

I'm developing a Java application that I'm going to need to extract the XML data from NFe,

Extract some data I can get by mapping the Tags as the sending recipient among others.

But when I need to extract the NFe products I can not.

The following is an excerpt of products from an NFe XML.

nfe.xml

<det nItem="1">
    <prod>
      <cProd>121402233</cProd>
      <cEAN>7898950236241</cEAN>
      <xProd>Michaelis Dicionario Escolar Da Lingua Portuguesa 1a Ed</xProd>
      <NCM>49019900</NCM>
      <CFOP>5102</CFOP>
      <uCom>PC</uCom>
      <qCom>1</qCom>
      <vUnCom>32.90</vUnCom>
      <vProd>32.90</vProd>
      <cEANTrib>7898950236241</cEANTrib>
      <uTrib>PC</uTrib>
      <qTrib>1</qTrib>
      <vUnTrib>32.90</vUnTrib>
      <vFrete>3.95</vFrete>
      <vDesc>3.95</vDesc>
      <indTot>1</indTot>
    </prod>
    <imposto>
      <ICMS>
        <ICMS40>
          <orig>0</orig>
          <CST>40</CST>
        </ICMS40>
      </ICMS>
      <PIS>
        <PISNT>
          <CST>06</CST>
        </PISNT>
      </PIS>
      <COFINS>
        <COFINSNT>
          <CST>06</CST>
        </COFINSNT>
      </COFINS>
    </imposto>
    <infAdProd>Desconto Incondicional Concedido: R$ 3.95</infAdProd>
  </det>

Below is an excerpt of the code that reads an NFe tag.

ReadingXml.java

public class LeituraXml {

private SAXBuilder sb;
private Document d;
private Element nfe;

public LeituraXml(String arquivo) {

    try {
        sb = new SAXBuilder();
        d = sb.build(new File(arquivo));
        nfe = d.getRootElement();


    } catch (Exception e) {

        JOptionPane.showMessageDialog(null, "Exceção ao processar arquivo! " + e.getMessage());
    }
}

public String getNumeroNFe() {
    try {

        XPath nNF = XPath.newInstance("//k:nfeProc/k:NFe/k:infNFe/k:ide/k:nNF");
        nNF.addNamespace("k", d.getRootElement().getNamespaceURI());
        Element node = (Element) nNF.selectSingleNode(d.getRootElement());

        return node.getText();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Erro ao processar arquivo! " + e.getMessage());
        return null;
    }
}

}
    
asked by anonymous 07.05.2015 / 21:59

1 answer

1

To extract the NFE data, I used a different form than the one I described above.

1st Download the NFE layout files on the site in the XML Schemas part Site NFE Farm I made the process of transforming XSD files to XML and put it in a directory of my project.

After this I performed the process of reading the NFE data as follows:

Xml.java Entry

....
   public void LerXml(String xml) {


    // Caminho do arquivo XML da NFe
    String xmlFilePathNFe3 = xml;
    JAXBContext context = null;
    TNfeProc tNfeProc = null;

    try {
        // Realizando o parser do XML da NFe para Objeto Java
        context = JAXBContext.newInstance(TNfeProc.class.getPackage().getName());

        Unmarshaller unmarshaller1 = context.createUnmarshaller();

        // Este é o seu Objeto Java da NFe (tNfeProc)
        tNfeProc = (TNfeProc) unmarshaller1.unmarshal(new File(xmlFilePathNFe3));

    } catch (JAXBException ex) {
        Logger.getLogger(TelaCompra.class.getName()).log(Level.SEVERE, null, ex);
    }

     for (TNFe.InfNFe.Det item : tNfeProc.getNFe().getInfNFe().getDet()) {

            JOptionPane.showMessageDialog(null, item.getProd().getCProd());
            JOptionPane.showMessageDialog(null, item.getProd().getXProd());

    }
  }
 ...
    
30.09.2015 / 16:14