4

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.

pHneutre
  • 1,091
  • 3
  • 12
  • 29

1 Answers1

4

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).
rolfl
  • 17,539
  • 7
  • 42
  • 76
  • @pHCito don't kick yourself over this one. These things happen, and it is why a second set of eyes helps. The reason for SO (the right reason). (can you tell that I have been stuck by things like this before...?) – rolfl Apr 07 '14 at 13:03