Reputation: 3890
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 I crafted up the code below with a direct link to my xml
file online. I am able to print the XML
to screen with no problems. However when I call my parse method I get Premature end of file.
It works if I pass the URL directly:
but fails when I passed an InputStream:
builder.parse(connection.getInputStream());
try {
URL url = new URL(xml);
URLConnection uc = url.openConnection();
HttpURLConnection connection = (HttpURLConnection )uc;
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream instream;
InputSource source;
//get XML from InputStream
if(connection.getResponseCode()>= 200){
connection.connect();
instream = connection.getInputStream();
parseDoc(instream);
}
else{
instream = connection.getErrorStream();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
static void parseDoc(InputStream instream) throws ParserConfigurationException,
SAXException, IOException{
BufferedReader buff_read = new BufferedReader(new InputStreamReader(instream,"UTF-8"));
String inputLine = null;
while((inputLine = buff_read.readLine())!= null){
System.out.println(inputLine);
}
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
factory.isIgnoringElementContentWhitespace();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(instream);
}
The errors I am getting:
[Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at com.ameba.api.network.MainApp.parseDoc(MainApp.java:78)
at com.ameba.api.network.MainApp.main(MainApp.java:41)
Upvotes: 34
Views: 329135
Reputation: 11597
Credits to @Sai for researching and sharing this analysis.
In my case it was because of checkstyle
failure.
The console output of build looked something like this
BUILD FAILED
/Users/username/buildtool-pkg-cache/packages/PackageName/PackageName-1.0.4447.0/AL2_x86_64/DEV.STD.PTHREAD/build/antfiles/as-checkstyle.xml:22: The following error occurred while executing this line:
/Users/username/buildtool-pkg-cache/packages/CheckstyleAntBuildLogic/CheckstyleAntBuildLogic-1.10.311291.0/AL2_x86_64/DEV.STD.PTHREAD/build/antfiles/checkstyle.xml:92: javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.
at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:792)
at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:395)
..
---------
javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.
at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getDOM(TransformerImpl.java:603)
at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:782)
..
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:284)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:101)
Caused by: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.
at java.xml/com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager.getDTM(XSLTCDTMManager.java:426)
at java.xml/com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager.getDTM(XSLTCDTMManager.java:214)
at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getDOM(TransformerImpl.java:581)
... 36 more
---------
com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.
at java.xml/com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager.getDTM(XSLTCDTMManager.java:426)
at java.xml/com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager.getDTM(XSLTCDTMManager.java:214)
at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getDOM(TransformerImpl.java:581)
org.xml.sax.SAXParseException; systemId: file:/Users/username/code/workplace/Project/build/Module/Module-1.0/AL2_x86_64/DEV.STD.PTHREAD/build/buildtool-documentation/checkstyle/checkstyle_report.xml; lineNumber: 1; columnNumber: 1; Premature end of file.
Upon scrolling up, I could see the real error appearing as benign console debug log
incremental-checkstyle:
[checkstyle] Running checkstyle only on changed lines of code.
[checkstyle] Running checkstyle in incremental mode
[checkstyle] Gathering git changes.
[checkstyle] Running Checkstyle 8.7 on 2 files
[checkstyle] /Users/username/code/workplace/Project/src/Module/src/activity/com/company/org/vertical/activity/ClassName.java:129:102: expecting RPAREN, found 'emailAddr'
[checkstyle] java.lang.reflect.InvocationTargetException
[xslt] Processing /Users/username/code/workplace/Project/build/Module/Module-1.0/AL2_x86_64/DEV.STD.PTHREAD/build/buildtool-documentation/checkstyle/checkstyle_report.xml to /Users/username/code/workplace/Project/build/Module/Module-1.0/AL2_x86_64/DEV.STD.PTHREAD/build/buildtool-documentation/checkstyle/checkstyle_report.html
[xslt] Loading stylesheet checkstyle.xsl
[xslt] : Error! Premature end of file.
[xslt] : Error! com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.
[xslt] Failed to process /Users/username/code/workplace/Project/build/Module/Module-1.0/AL2_x86_64/DEV.STD.PTHREAD/build/buildtool-documentation/checkstyle/checkstyle_report.xml
So the problem is actually
ClassName.java:129:102: expecting RPAREN, found 'emailAddr'
The culprit line contained an if
-expression with instanceof
operator
if (newEmailAddr instanceof com.company.org.vertical.types.getAddrs.EmailAddr emailAddr) {
..
}
Upon researching we found that checkstyle
doesn't seem to like instanceof
operator much.
So our options were either to replace instance
of expression with something like equals(..)
or just ignore this failure. We decided to ignore this failure for the time being.
Upvotes: 0
Reputation: 1
<?xml version="1.0" encoding="UTF-8"?>
Make sure to insert the heading properly at the top level and it should not point to any descendant within your XML file.
Upvotes: 0
Reputation: 51
Use inputstream once don't use it multiple times and Do inputstream.close()
Upvotes: 1
Reputation: 11
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);
Upvotes: 1
Reputation: 1
One of the other reason is , you should whitelist your IP address (IPv4) in your mongodb settings. Hope it resolves !
Upvotes: -4
Reputation: 1223
For those who reached this post for Answer:
This happens mainly because the InputStream
the DOM parser is consuming is empty
So in what I ran across, there might be two situations:
InputStream
you passed into the parser has been used and thus emptied.File
or whatever you created the InputStream
from may be an empty file or string or whatever. The emptiness might be the reason caused the problem. So you need to check your source of the InputStream
.Upvotes: 4
Reputation: 107
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.
Upvotes: 6
Reputation: 95948
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.
Upvotes: 1
Reputation: 25140
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.
Upvotes: 40