Extract products from an Nfe XML

Asked

Viewed 1,716 times

0

I am developing a Java application that I will need to extract XML data from Nfe,

Extract some data I can map the Tags as issuer recipient among others.

But when I need to extract products from Nfe I cannot.

Below is a product snippet 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 from the code that reads a field(tag) of Nfe.

Leituraxml.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;
    }
}

}

1 answer

1


To extract the data from NFE I used another way other than the one described above.

1º I downloaded the NFE layout files on the site in the part XML Schemas NFE Fazenda website I did the process of transforming the XSD files to XML and put in a directory of my project.

After that I carried out the process of reading the NFE data as follows:

Entradaxml.java

....
   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());

    }
  }
 ...
  • What is the purpose of transforming XSD files into XML files?

Browser other questions tagged

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