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