简体   繁体   中英

why i am getting this exception while reading xml from a thid party url

i am writing a program to connect to a third party url and read xml from there. Although, it works very well on my local machine on tomcat server in windows environment, but when i deploy it on weblogic in linux environment, i get anexception. I would appreciate appreciate your help. Thanks in advance.

Here is the code snippet:

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setRequestMethod("GET");
      connection.setDoInput(true);
   connection.connect();

   log.info("Confirmation URL getURL(): "+connection.getURL());
   log.info("Confirmation URL getContentEncoding: "+connection.getContentEncoding());
   log.info("Confirmation URL getContentType: "+connection.getContentType());
   log.info("Confirmation URL getContentLength: "+connection.getContentLength());
   log.info("Confirmation URL getContent(): "+connection.getContent());
   log.info("Confirmation URL getAllowUserInteraction(): "+connection.getAllowUserInteraction());
   log.info("Confirmation URL getClass(): "+connection.getClass());
   log.info("Confirmation URL getHeaderFields(): "+connection.getHeaderFields());
   Map<String,List<String>> requestProperties = connection.getRequestProperties();

   if(requestProperties!=null){
    Iterator<String> keysIterator = requestProperties.keySet().iterator();
    while (keysIterator.hasNext()) {
     String key = (String) keysIterator.next();
     log.info("********* RequestProperty "+key+" : "+requestProperties.get(key));
    }
   }
   else {
    log.info("requestProperties is null ");
   }



      InputStream xmlcontent = connection.getInputStream();

   Date date = new Date();

   DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
     .newInstance();
   DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
   Document doc = docBuilder.parse(xmlcontent); // <-- exception thrown here

Following is the exception I get:

org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
 at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1414)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(XMLDocumentScannerImpl.java:1423)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
 at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
 at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235)
 at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
 at weblogic.xml.jaxp.RegistryDocumentBuilder.parse(RegistryDocumentBuilder.java:163)
 at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
 at com.scea.shoppingcart.controller.OrderConfirmationTask.readXml(OrderConfirmationTask.java:337)
 at com.scea.shoppingcart.controller.OrderConfirmationTask.getFeed(OrderConfirmationTask.java:213)
 at com.scea.shoppingcart.controller.OrderConfirmationTask.generateUrlToHit(OrderConfirmationTask.java:161)
 at com.scea.shoppingcart.controller.OrderConfirmationTask.processRequest(OrderConfirmationTask.java:112)
 at com.scea.shoppingcart.controller.OrderConfirmationJob.executeInternal(OrderConfirmationJob.java:22)
 at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
 at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
 at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
2011-01-20 16:06:01,539 [ERROR] - ==Exception processing Order Confirmation XML !!!==The markup in the document following the root element must be well-formed.=stackTrace=org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
 at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1414)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(XMLDocumentScannerImpl.java:1423)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
 at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
 at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235)
 at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
 at weblogic.xml.jaxp.RegistryDocumentBuilder.parse(RegistryDocumentBuilder.java:163)
 at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
 at com.scea.shoppingcart.controller.OrderConfirmationTask.readXml(OrderConfirmationTask.java:337)
 at com.scea.shoppingcart.controller.OrderConfirmationTask.getFeed(OrderConfirmationTask.java:213)
 at com.scea.shoppingcart.controller.OrderConfirmationTask.generateUrlToHit(OrderConfirmationTask.java:161)
 at com.scea.shoppingcart.controller.OrderConfirmationTask.processRequest(OrderConfirmationTask.java:112)
 at com.scea.shoppingcart.controller.OrderConfirmationJob.executeInternal(OrderConfirmationJob.java:22)
 at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
 at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
 at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)

The exception states that

The markup in the document following the root element must be well-formed.

Thus I would expect that the document coming through is invalid in that it isn't well-formed XML. In order to address this, it might be a good idea to capture the output of the stream into a string (or redirect it to a file, etc.) during debugging, so that you can investigate exactly which characters make it through to the other side.

There are multiple reasons why this could be happening (eg you're getting a 404/500 error from the webserver - an HTML page is almost certainly "nearly XML" but badly-formed). You'll need to see the actual data to work out what's going on, as I expect that it really is an error message from the server.

Thanks for replying. Indeed server status returned was 301, so after getting clue from one of the blogs(http://tech.rationalaspect.com/2010/10/301-moved-permanently-urlconnection-and.html) while googling, I simply appended a '/' at the end of the base url, and it worked for me. Although, I am not absolutely sure what difference it made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM