Olá pessoal do fórum, antes de mais nada quero dizer que procurei durante varios dias uma solução e não encontrei por isso recorri ao forum para tentar um esclarecimento do meu problema que consiste na seguinte situação; tenho um metodo que gera os dados referentes a consulta de uma NF-e no webservice NfeConsulta2 que esta listado abaixo:
package br.com.ncmhelper.nfe.status.testes;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.util.AXIOMUtil;
import br.com.ncmhelper.manifesto.utils.AssinarDocumento;
import br.com.ncmhelper.nfe.status.vo.ConsSitNFe;
import br.com.ncmhelper.nfe.status.vo.ProtNFe;
import br.com.ncmhelper.nfe.status.vo.RetCancNFe;
import br.com.ncmhelper.nfe.status.vo.RetConsSitNFe;
import br.inf.portalfiscal.www.nfe.wsdl.nfeconsulta2.NfeConsulta2Stub;
import br.inf.portalfiscal.www.nfe.wsdl.nfeconsulta2.NfeConsulta2Stub.NfeCabecMsg;
import br.inf.portalfiscal.www.nfe.wsdl.nfeconsulta2.NfeConsulta2Stub.NfeCabecMsgE;
import br.inf.portalfiscal.www.nfe.wsdl.nfeconsulta2.NfeConsulta2Stub.NfeConsultaNF2Result;
import br.inf.portalfiscal.www.nfe.wsdl.nfeconsulta2.NfeConsulta2Stub.NfeDadosMsg;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.CompactWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class Teste {
private static XStream xt = new XStream(new DomDriver("UTF-8"));
public static void main(String[] args) throws XMLStreamException {
xt.autodetectAnnotations(true);
xt.processAnnotations(new Class[] {ConsSitNFe.class, ProtNFe.class, RetCancNFe.class, RetConsSitNFe.class});
ByteArrayOutputStream out;
Writer writer;
try {
if (AssinarDocumento.assinar("/tmp/certificado.pfx", "senha")) {
out = new ByteArrayOutputStream();
writer = new OutputStreamWriter(out, "UTF-8");
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
ConsSitNFe consultar = new ConsSitNFe();
consultar.setChNFe("00000000000000000000000000000000000000000000"); // apenas um exemplo de chave de acesso!!!
consultar.setTpAmb(1);
consultar.setVersao("3.00");
xt.marshal(consultar, new CompactWriter(writer));
String dadosMsg = new String(out.toByteArray(), "UTF-8");
System.out.println(dadosMsg);
OMElement element = AXIOMUtil.stringToOM(dadosMsg);
NfeConsulta2Stub.NfeDadosMsg nfe = new NfeDadosMsg();
nfe.setExtraElement(element);
System.out.println(element.toString());
NfeCabecMsg cabecalho = new NfeCabecMsg();
cabecalho.setCUF("51");
cabecalho.setVersaoDados("3.00");
NfeCabecMsgE cabecalhoE = new NfeCabecMsgE();
cabecalhoE.setNfeCabecMsg(cabecalho);
NfeConsultaNF2Result retorno = new NfeConsulta2Stub(new URL("https://nfe.sefaz.mt.gov.br/nfews/v2/services/NfeConsulta2?wsdl").toString()).nfeConsultaNF2(nfe, cabecalhoE);
File xml = File.createTempFile("xml_tmp", "xml");
xml.deleteOnExit();
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xml)));
bfw.write(retorno.getExtraElement().toString());
bfw.close();
RetConsSitNFe ret = (RetConsSitNFe) xt.fromXML(xml);
System.out.println(xt.toXML(ret));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
a saida do primeiro System.ou.println na linha 61 é:
<?xml version="1.0" encoding="UTF-8"?><consSitNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.01"><tpAmb>1</tpAmb><xServ>CONSULTAR</xServ><chNFe>00000000000000000000000000000000000000000000</chNFe></consSitNFe>
e da segunda é:
<consSitNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.01"><tpAmb>1</tpAmb><xServ>CONSULTAR</xServ><chNFe>00000000000000000000000000000000000000000000</chNFe></consSitNFe>
e o resultado após a consulta no ws é:
<retConsSitNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="3.00">
<tpAmb>1</tpAmb>
<chNFe>$entidade.chNFe</chNFe>
<verAplic>3.00</verAplic>
<cStat>242</cStat>
<xMotivo>Rejeicao: Cabecalho - Falha no Schema XML</xMotivo>
<cUF>51</cUF>
</retConsSitNFe>
ou seja, oque esta acontecendo é que quando o metodo OMElement element = AXIOMUtil.stringToOM(dadosMsg); e chamado ele remove a primeira linha do xml que estou gerando, e já procurei uma solução na internet durante varios dias, pesquisei li reli tópicos e documentações e nao entendi o que esta acontecendo! gostaria de saber se algum dos colegas podem me ajudar nisso??
ai tentei utlizar o codigo abaixo para substituir o metodo das linha 63 à 65:
ByteArrayInputStream bais = new ByteArrayInputStream(dadosMsg.getBytes());
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(bais);
NfeConsulta2Stub.NfeDadosMsg nfe = NfeConsulta2Stub.NfeDadosMsg.Factory.parse(reader);
porem o sistema gera um Exception ao chegar na linha [i]NfeConsulta2Stub.NfeDadosMsg nfe = NfeConsulta2Stub.NfeDadosMsg.Factory.parse(reader);[/i] com a seguinte descrição:
novamente busquei na internet uma solução e infelizmente não encontrei!!!
Desde já agradeço a ajuda!!
Att,
André Dalcin
Planos Assessoria