I apologize for asking the same question, but as per my research it seemed in different context. I know why the error is coming but I failed to eradicate it without looking and following some programming forums. My program goes as follows: I am pinging a url and opening input stream, then that stream is written into an xml file. After that I am using Xpath to extract some information that is further used for some computation. My code is as follows:
URL u=new URL("url here");
HttpURLConnection con=(HttpURLConnection)u.openConnection();
InputStream is=con.getInputStream();
BufferedInputStream br=new BufferedInputStream(is);
File f=new File("data.xml");
if(f.exists())f.delete();
f.createNewFile();
byte data[] = new byte[1024];
int count;
FileOutputStream fout = new FileOutputStream(f);
while((count = br.read(data,0,1024)) != -1)
{
fout.write(data, 0, count);
}
fout.close();
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse("data.xml");
XPathFactory factory=XPathFactory.newInstance();
XPath xPath=factory.newXPath();
XPathExpression expr = xPath.compile("//tr/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
when executing this code I get : White spaces are required between publicId and systemId.
Since I am directly writing to the file, where can this white space error possibly occur?
It looks like your previously written XML file is invalid, this is why you get an error parsing the file. The standard XML parsing frameworks accept only well-formed valid XML.
If you want to work with invalid, maybe unbalanced, HTML tag-soups, I recommend JSoup as it can work with invalid XML.