Best Way to Use InputStream Regarding Stability and XML

I have a REST web service that listens for POST requests and captures the XML payload from the client and first saves it as an InputStream, that is, on a view object that you can call getStream () on.

I want to use the XML stored in the InputStream, and I'm starting to think it would be wise to continue it, so I can interrogate the data several times - as soon as you read it, the object will become null. So I thought about converting InputStream to string. This is not a good idea, since DocumentBuilder.parse () from the javax.xml.parsers library will only let you pass:

  • Inputstreams
  • Files
  • Url
  • SAX InputSources

not strings.

What should I do with InputStreams regarding XML parsing from it? Whereas I want to re-interrogate this XML in future processes using code.

+3
source share
6 answers

If you have an InputStream and want to use it as an XML document, why don't you just parse it and go around the Document object? If you want to save this object, use serializers to write it as text.

As I noted in my commentary on Tom Hawtin, coding is very important when working with XML. Instead of writing a long entry here that might miss your specific situation, here is an article I wrote.

: , -, . , : XML Content-Type. XML - , , , . : -, , , /xml ( , , , ). - , application/xml utf-8. , , , - , .

+2

, , , . , . , XML .

, ( , ), . ByteArrayInputStream .

. -, , . -, XML , , , .

Edit:

, ( ) InputStream available(), , . InputStream DataInputStream, readFully(), .

:

. , () .

+1

Apache Commons IO. IOUtils InputStreams String .

+1

, , (.. ). byte[] ( !), , .

InputStream ByteArrayOutputStream ( read()) byte[] .

0

XML , InputStream ( ), ?

0

java.io.StringReader InputSource.

You might want to save the data in byte[], and then read with ByteArrayInputStream. If this is especially important, you might want to consider compressing. This can be read iwth GzipInputStream, which often needs to be wrapped in BufferedInputStream.

-2
source

Source: https://habr.com/ru/post/1704647/


All Articles