Why am I getting this error Premature end of file?

后端 未结 7 2036
滥情空心
滥情空心 2020-12-16 10:40

I am trying to parse an XML response, but I am failing miserably. I thought initially that the xml was just not being returned in the response, so

相关标签:
7条回答
  • 2020-12-16 11:13

    Use inputstream once don't use it multiple times and Do inputstream.close()

    0 讨论(0)
  • 2020-12-16 11:14

    When you do this,

    while((inputLine = buff_read.readLine())!= null){
            System.out.println(inputLine);
        }
    

    You consume everything in instream, so instream is empty. Now when try to do this,

    Document doc = builder.parse(instream);
    

    The parsing will fail, because you have passed it an empty stream.

    0 讨论(0)
  • 2020-12-16 11:15

    One of the other reason is , you should whitelist your IP address (IPv4) in your mongodb settings. Hope it resolves !

    0 讨论(0)
  • 2020-12-16 11:26

    I came across the same error, and could easily find what was the problem by logging the exception:

    documentBuilder.setErrorHandler(new ErrorHandler() {
        @Override
        public void warning(SAXParseException exception) throws SAXException {
            log.warn(exception.getMessage());
        }
    
        @Override
        public void fatalError(SAXParseException exception) throws SAXException {
            log.error("Fatal error ", exception);
        }
    
        @Override
        public void error(SAXParseException exception) throws SAXException {
            log.error("Exception ", exception);
        }
    });
    

    Or, instead of logging the error, you can throw it and catch it where you handle the entries, so you can print the entry itself to get a better indication on the error.

    0 讨论(0)
  • 2020-12-16 11:26

    I resolved the issue by converting the source feed from http://www.news18.com/rss/politics.xml to https://www.news18.com/rss/politics.xml

    with http below code was creating an empty file which was causing the issue down the line

        String feedUrl = "https://www.news18.com/rss/politics.xml"; 
        File feedXmlFile = null;
    
        try {
        feedXmlFile =new File("C://opinionpoll/newsFeed.xml");
        FileUtils.copyURLToFile(new URL(feedUrl),feedXmlFile);
    
    
              DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
              DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
              Document doc = dBuilder.parse(feedXmlFile);
    
    0 讨论(0)
  • 2020-12-16 11:32

    You are getting the error because the SAXBuilder is not intelligent enough to deal with "blank states". So it looks for at least an <xml ..> declaration, and when that causes a no data response it creates the exception you see rather than report the empty state.

    0 讨论(0)
提交回复
热议问题