Bom dia.
Estou criando um exemplo em java para leitura de um arquivo XML referente À NFe. Estou montando o XML aos poucos e testando junto do exemplo que fiz. Entretanto, quando aparece elementos que se repetem, exemplo:
<det nItem="1">
<prod>
<cProd>049011</cProd>
<cEAN>7897283490283</cEAN>
<xProd>6010 - PONCHITO LIMONADA (12X450ML)</xProd>
<NCM>22029000</NCM>
<CFOP>5405</CFOP>
<uCom>CX</uCom>
<qCom>1.0000</qCom>
<vUnCom>9.360000</vUnCom>
<vProd>9.36</vProd>
<cEANTrib>7897283490283</cEANTrib>
<uTrib>CX</uTrib>
<qTrib>1.0000</qTrib>
<vUnTrib>9.360000</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
<ICMS>
<ICMS60>
<orig>0</orig>
<CST>60</CST>
<vBCSTRet>0.00</vBCSTRet>
<vICMSSTRet>0.00</vICMSSTRet>
</ICMS60>
</ICMS>
<IPI>
<cEnq>999</cEnq>
<IPINT>
<CST>53</CST>
</IPINT>
</IPI>
<PIS>
<PISOutr>
<CST>99</CST>
<vBC>8.37</vBC>
<pPIS>1.65</pPIS>
<vPIS>0.13</vPIS>
</PISOutr>
</PIS>
<COFINS>
<COFINSOutr>
<CST>99</CST>
<vBC>8.37</vBC>
<pCOFINS>7.60</pCOFINS>
<vCOFINS>0.63</vCOFINS>
</COFINSOutr>
</COFINS>
</imposto>
</det>
<det nItem="2">
<prod>
<cProd>049011</cProd>
<cEAN>7897283490283</cEAN>
<xProd>6010 - PONCHITO LIMONADA (12X450ML)</xProd>
<NCM>22029000</NCM>
<CFOP>5405</CFOP>
<uCom>CX</uCom>
<qCom>1.0000</qCom>
<vUnCom>9.360000</vUnCom>
<vProd>9.36</vProd>
<cEANTrib>7897283490283</cEANTrib>
<uTrib>CX</uTrib>
<qTrib>1.0000</qTrib>
<vUnTrib>9.360000</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
<ICMS>
<ICMS60>
<orig>0</orig>
<CST>60</CST>
<vBCSTRet>0.00</vBCSTRet>
<vICMSSTRet>0.00</vICMSSTRet>
</ICMS60>
</ICMS>
<IPI>
<cEnq>999</cEnq>
<IPINT>
<CST>53</CST>
</IPINT>
</IPI>
<PIS>
<PISOutr>
<CST>99</CST>
<vBC>8.37</vBC>
<pPIS>1.65</pPIS>
<vPIS>0.13</vPIS>
</PISOutr>
</PIS>
<COFINS>
<COFINSOutr>
<CST>99</CST>
<vBC>8.37</vBC>
<pCOFINS>7.60</pCOFINS>
<vCOFINS>0.63</vCOFINS>
</COFINSOutr>
</COFINS>
</imposto>
</det>
tenho este tipo de erro no console do NetBeans:
Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$DuplicateFieldException: det
---- Debugging information ----
duplicate-field : det
class : model.NfeProc
required-type : model.InfNFe
path : /nfeProc/NFe/infNFe/det[2]
-------------------------------
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$SeenFields.add(AbstractReflectionConverter.java:322)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:246)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:218)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:246)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:218)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:137)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:33)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853)
at controller.NfeProcController.getNfeProc(NfeProcController.java:21)
at teste.Main.main(Main.java:14)
Java Result: 1
Ate sei que não programei a aplicação para receber este tipo de XML. Mas ai que está, não faço ideia de como resolver este problema.
Para cada elemento que agrupa outros elementos tenho 1 classe fazendo composições.
Exemplo:
package model;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("nfeProc")
public class NfeProc
{
private NFe NFe;
public model.NFe getNFe() {
return NFe;
}
public void setNFe(model.NFe NFe)
{
this.NFe = NFe;
}
}
[code]package model;
public class NFe
{
private InfNFe infNFe;
public InfNFe getInfNFe() {
return infNFe;
}
public void setInfNFe(InfNFe infNFe) {
this.infNFe = infNFe;
}
}[/code]
E por ai vai…nem sei se é a melhor forma de realizar este exemplo.
Ai fiz o mapeamento para pegar os dados do XML e converter pra objeto:
Segue:
import java.io.IOException;
import model.NfeProc;
public class NfeProcController
{
public NfeProc getNfeProc(String caminhoXML) throws FileNotFoundException, IOException
{
XStream xstream = new XStream(new DomDriver());
xstream.alias("nfeProc", NfeProc.class);
xstream.processAnnotations(NfeProc.class);
BufferedReader br = new BufferedReader(new FileReader(caminhoXML));
NfeProc nfeProc = (NfeProc) xstream.fromXML(br);
br.close();
return nfeProc;
}
}
Acho que é nesta classe acima que devo alterar algo, mas não tenho ideia como fazer isso.
Tenho que criar outra classe para mapear elementos que se repetem?
Desculpa a ignorância pessoal, mas só falta isso pra mim resolver este problema, pois as classes de modelo já estão todas criadas.
Agradeço desde já