Search code examples
javajspservletssoapwsdl

Parse SOAP XML response, Error: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog


Expected Output:
- Get the Supplier Details by Entering the ZipCode as Input

Issue:
- Error showing at 3rd line of try catch block in SupplierDetail.java
- While unmarshaling it throws the Exception. See error log

javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]

WSDL:
Endpoint http://www.webservicex.net/medicareSupplier.asmx?WSDL

Use GetSupplierByZipCode Method to the Supplier Details from given WSDL

Step 1: Using wsimport tool generate stubs

wsimport -keep -d "path" http://www.webservicex.net/medicareSupplier.asmx?WSDL

Step 2: Created a Dynamic Web Project 1. Added the generated files to project

Below is the Code
index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MediCare SOAP Client </title>
</head>
<body>
<h2>Hello JSP</h2>

<form method="post" action="supplierdetail">
<label>Zip Code</label>
<input type="text" name="searchBarInputId">
<input type="submit" value="Get Supplier">
</form>

</body>
</html>

SupplierDetail.java

package com.supplier;

import java.io.IOException;
import java.io.StringReader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;


import net.webservicex.MediCareSupplier;
import net.webservicex.MediCareSupplierSoap;
import net.webservicex.SupplierData;

public class SupplierDetail extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cityZipCode = request.getParameter("searchBarInputId");
        System.out.println("doPost:"+ cityZipCode);
        MediCareSupplier mediCareObj  = new MediCareSupplier();
        MediCareSupplierSoap mediCareSupplier = mediCareObj.getMediCareSupplierSoap();
        mediCareSupplier.getSupplierByZipCode(cityZipCode, null, null);

        try {
            JAXBContext jAXBContext = JAXBContext.newInstance(SupplierData.class);
            Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
            SupplierData data = (SupplierData)unmarshaller.unmarshal(new StringReader(response.toString()));
            System.out.println("SupplierDAta: "+data+':'+ data.getSupplierNumber()+ "\n"+data.getAddress1());

            /* RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/SupplierDetails.jsp");
             dispatcher.forward(request, response);*/ 

        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }
}

Error Log:

INFO: Server startup in 610 ms doPost:61244 javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.] at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at com.supplier.SupplierDetail.doPost(SupplierDetail.java:36) at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source) Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) ... 28 more

Please help.. Thanks in Advance


Solution

  • Found another solution

    Returning Multiple values

    With SOAP it is possible to return multiple values in a single request. This is impossible in Java as a method can only return one object.

    JAX-WS solves this problem with the concept of Holders. A javax.xml.ws.Holder is a simple wrapper object that can be passed into the @WebService method as a parameter. The application sets the value of the holder during the request and the server will send the value back as an OUT parameter.

        MediCareSupplier mediCare = new MediCareSupplier();
        MediCareSupplierSoap mediCareSOAP = mediCare.getMediCareSupplierSoap();
    
        Holder<Boolean> getSupplierByZipCodeResult = new Holder<Boolean>();
        Holder<SupplierDataList> supplierDataLists = new Holder<SupplierDataList>();
        mediCareSOAP.getSupplierByZipCode(zip, getSupplierByZipCodeResult, supplierDataLists);
    
        List<SupplierData> supplierDataList = supplierDataLists.value.getSupplierDatas().getSupplierData();
    

    loop through the supplierDataList and get all the data