org.xml.sax.SAXParseException: Document root element is missing

Discussions

XML & Web services: org.xml.sax.SAXParseException: Document root element is missing

  1. I have a java file in which i parse a xml file URL url = new URL(address); con = (HttpURLConnection)(url.openConnection()); con.setDoOutput(true); con.setRequestMethod("GET"); in=con.getInputStream(); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(in); Error occurs in the last line only for some xml files. the xml files are well formed and is downloaded from google so no error can be in that , what may the reason for the error. the error is as follows org.xml.sax.SAXParseException: Document root element is missing at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3376) at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3364) at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:668) at org.apache.crimson.parser.Parser2.parse(Parser2.java:337) at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:448) at org.apache.crimson.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:185) at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:76) at com.df.semtools.adwords.GetAdwordsStats.getKeywords(GetAdwordsStats.java:278) .. ... .... Thanks for ur help in advance
  2. Are you parsing the whole document?[ Go to top ]

    Just a guess, but it sounds like what would happen if the parser was starting to parse before the document had downloaded. I don't know if an HTTP input stream will block the parser and wait for the content to download, or not. Just a thought, Cheers, Tony.
  3. The exception states there is no document root, so probably the document is empty. I would write the retrieved data to a logger first, so you can actually see what data the parser tries to parse...