Estou envelopando a informação a ser enviada no qual contêm o xml e o seguinte erro é retornado:
org.apache.axiom.soap.SOAPProcessingException: First Element must contain the local name, Envelope , but found EnviarLoteRpsEnvio
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.constructNode(StAXSOAPModelBuilder.java:305)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.createOMElement(StAXSOAPModelBuilder.java:252)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.createNextOMElement(StAXSOAPModelBuilder.java:234)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:249)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.getSOAPEnvelope(StAXSOAPModelBuilder.java:204)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.identifySOAPVersion(StAXSOAPModelBuilder.java:176)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.<init>(StAXSOAPModelBuilder.java:170)
at org.apache.axis2.saaj.SOAPPartImpl.<init>(SOAPPartImpl.java:194)
at org.apache.axis2.saaj.SOAPMessageImpl.<init>(SOAPMessageImpl.java:118)
at org.apache.axis2.saaj.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:132)
at nfse.schemas.webservice_teste.WebServiceNfse.create_soap_message(WebServiceNfse.java:94)
at nfse.schemas.webservice_teste.WebServiceNfse.montaEnvelopeSoap(WebServiceNfse.java:27)
at nfse.schemas.geraXML_nfse.<init>(geraXML_nfse.java:130)
at org.apache.jsp.mga.nota_005ffiscal_005fservicos.geraRegra_jsp._jspService(geraRegra_jsp.java:519)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
- Código:
-
package nfse.schemas.webservice_teste;
import java.util.Date;
import java.util.Iterator;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPException;
import javax.xml.soap.Node;
import javax.xml.soap.Name;
public class WebServiceNfse {
private static final String LocalName = "Envelope";
private static final String NamespacePrefx = "ns3";
private ByteArrayOutputStream out;
private ByteArrayInputStream in;
public SOAPEnvelope montaEnvelopeSoap(){
SOAPEnvelope env = null;
try{
SOAPMessage msg = create_soap_message(in,"C:\\TutorialArquivos\\nfseAss.xml");
env = msg.getSOAPPart().getEnvelope();
SOAPHeader hdr = env.getHeader();
Name lookup_name = create_qname(msg);
hdr.addHeaderElement(lookup_name).addTextNode("time_request");
out = new ByteArrayOutputStream();
msg.writeTo(out);trace("The sent SOAP message:", msg);
SOAPMessage response = process_request();
extract_contents_and_print(response);
}catch(SOAPException e)
{
System.err.println(e);
}catch(IOException e)
{
System.err.println(e);
}
return env;
}
private SOAPMessage process_request() {
process_incoming_soap();
coordinate_streams();
return create_soap_message(in,"C:\\TutorialArquivos\\nfseAss.xml");
}
private void process_incoming_soap() {
try {
coordinate_streams();
SOAPMessage msg = create_soap_message(in,"C:\\TutorialArquivos\\nfseAss.xml");
Name lookup_name = create_qname(msg);
SOAPHeader header = msg.getSOAPHeader();
Iterator<?> it = header.getChildElements(lookup_name);
Node next = (Node) it.next();
String value = (next == null) ? "Error!" : next.getValue();
if (value.toLowerCase().contains("time_request")) {
String now = new Date().toString();
SOAPBody body = msg.getSOAPBody();
body.addBodyElement(lookup_name).addTextNode(now);
msg.saveChanges();
msg.writeTo(out);trace("The received/processed SOAP message:", msg);
}
}catch(SOAPException e)
{
System.err.println(e);
}catch(IOException e)
{
System.err.println(e);
}
}
private void extract_contents_and_print(SOAPMessage msg) {
try {
SOAPBody body = msg.getSOAPBody();
Name lookup_name = create_qname(msg);
Iterator<?> it = body.getChildElements(lookup_name);
Node next = (Node) it.next();
String value = (next == null) ? "Error!" : next.getValue();
System.out.println("\n\nReturned from server: " + value);
}
catch(SOAPException e) {
System.err.println(e);
}
}
private SOAPMessage create_soap_message(InputStream in,String nfseXml)
{
SOAPMessage msg = null;;
try {
in =new BufferedInputStream(new FileInputStream(nfseXml));
MessageFactory mt = MessageFactory.newInstance();
stub._getServiceClient().getOptions().
setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, Boolean.FALSE);
msg = mt.createMessage(null, in);
}catch(SOAPException e)
{
System.err.println(e);
}catch(IOException e)
{
System.err.println(e);
}
return msg;
}
private Name create_qname(SOAPMessage msg) {
Name name = null;
try {
SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
name = env.createName(LocalName, NamespacePrefx, Namespace);
}
catch(SOAPException e) {
System.err.println(e);
}
return name;
}
private void trace(String s, SOAPMessage m)
{
System.out.println("\n");
System.out.println(s);
try {
m.writeTo(System.out);
}catch(SOAPException e)
{
System.err.println(e);
}catch(IOException e) {
System.err.println(e);
}
}
private void coordinate_streams() {
in = new ByteArrayInputStream(out.toByteArray());
out.reset();
}
private SOAPMessage create_soap_message() {
SOAPMessage msg = null;
try {
MessageFactory mf = MessageFactory.newInstance();
msg = mf.createMessage();
}
catch(SOAPException e) {
System.err.println(e); }return msg;
}
}