简体   繁体   English

为什么我在从第三方URL读取xml时遇到此异常

[英]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. 我正在编写一个程序以连接到第三方URL并从那里读取xml。 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. 虽然,它在Windows环境中的tomcat服务器上的本地计算机上运行良好,但是当我在linux环境中的weblogic上部署它时,出现异常。 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. 因此,我希望通过的文档无效,因为它不是格式正确的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). 发生这种情况的原因有多种(例如,您从网络服务器收到404/500错误-HTML页面几乎可以肯定是“几乎XML”但格式错误)。 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. 确实返回的服务器状态为301,因此在谷歌搜索时从其中一个博客(http://tech.rationalaspect.com/2010/10/301-moved-permanently-urlconnection-and.html)获得线索后,我只是附加了一个基本网址末尾的'/'对我有用。 Although, I am not absolutely sure what difference it made. 虽然,我不确定这有什么区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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