In Java, how to create a temporary file only in memory for xml parsing?

I would like to read in the XML response and make a temporary in-memory file from xml. Then I would like to read in the file to see if certain elements exist. After that, I just want to get rid of the temp file. I am familiar with creating and reading files to / from the file system, is it possible not to write, and then read from a file only in memory?

+5
source share
3 answers

Why are you trying to create it as a β€œfile” in memory? Just save it as an XML representation (whether using the JDOM, WOM or DOM API).

, "" . , , , , , !

+4

? .

, , ByteArrayOutputStream ByteArrayInputStream //. , ; , , , , .

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer w = new OutputStreamWriter(baos);
w.write(...);
byte[] bytes = baos.toByteArray();

, ByteBuffer , .

, , , .

+4

I do not see the need to create a temporary file just to check for the presence of certain elements. Most XML parsers let you read directly from some input stream. All you need to do is convert the XML response string to the input stream, and then pass it to some XML parser to perform validation: -

// converting string to input stream
InputStream is = new ByteArrayInputStream( myString.getBytes( charset ) );
0
source

All Articles