I'm getting that exception when reading an InputStream with JDOM's SAXBuilder's build method:

InputStream bais = p_sendXML.getXml().getInputStream();
File myFile = new File(System.getProperty("java.io.tmpdir"), PREFIX+p_sendXML.getSessionId()+".xml");
IOUtils.copy(bais, new FileOutputStream(myFile));
LOGGER.debug("File save in: "+myFile.getAbsolutePath());
SAXBuilder builder = new SAXBuilder();
Document xmlDoc = builder.build(bais);

The File is correctly created and the XML inside is valid, so I shouldn't get this exception. There is a new line at the end of the XML file, if you are wondering.

有帮助吗?

解决方案

You have 'exhausted' the bais when you did IOUtils.copy(bais, new FileOutputStream(myFile));. You have copied the contents of the bais to the file, and now the bais is 'empty'. You will need to either:

  1. take a copy of the bais somehow before writing it to disk
  2. Parse it directly by JDOM and use JDOM to write the XML to disk (XMLOutputter)
  3. get JDOM to parse the file (not the bais).
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top