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


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( at org.apache.crimson.parser.Parser2.fatal( at org.apache.crimson.parser.Parser2.parseInternal( at org.apache.crimson.parser.Parser2.parse( at org.apache.crimson.parser.XMLReaderImpl.parse( at org.apache.crimson.jaxp.DocumentBuilderImpl.parse( at javax.xml.parsers.DocumentBuilder.parse( at com.df.semtools.adwords.GetAdwordsStats.getKeywords( .. ... .... 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...