문제

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