How can I create an InputStream object from a XML Document or Node object to be used in xstream? I need to replace the ??? with some meaningful code. Thanks.
Document doc = getDocument(); InputStream is = ???; MyObject obj = (MyObject) xstream.fromXML(is);
To avoid downloading a document multiple times, we cache the document locally using an abstraction called a Document Input Stream (DIS). A DIS is an input stream that caches the entire contents of the document read from the internet. It also provides methods to re-read the document.
Java InputStream 's are used for reading byte based data, one byte at a time. Here is a Java InputStream example which reads all the bytes from a file: InputStream inputstream = new FileInputStream("c:\\data\\input-text. txt"); int data = inputstream.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(doc); Result outputTarget = new StreamResult(outputStream); TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With