Code example for StreamSource

Methods: getInputStream, getReader

0
                // Instead, we write the Document to a Stream, and validate that 
                Transformer transformer = transformerFactory.newTransformer();
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                transformer.transform(domSource, new StreamResult(outputStream));
                ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
                validateStreamSource(new StreamSource(inputStream), parser, errorHandler);
            } 
            catch (TransformerException ex) {
                throw new XmlValidationException("Could not validate DOM source: " + ex.getMessage(), ex);
            } 
 
        } 
 
        private void validateStreamSource(StreamSource streamSource,
                                          SAXParser parser,
                                          DefaultValidationErrorHandler errorHandler) throws SAXException, IOException {
            if (streamSource.getInputStream() != null) {
                parser.parse(streamSource.getInputStream(), errorHandler);
            } 
            else if (streamSource.getReader() != null) {
                parser.parse(new InputSource(streamSource.getReader()), errorHandler);
            } 
            else { 
                throw new IllegalArgumentException("StreamSource contains neither InputStream nor Reader");
            } 
        }