/** * Create a document with the root element of the form <someElement xmlns="customNamespace" * * @param baseNamespace * @return * @throws ProcessingException */ public static Document createDocumentWithBaseNamespace(String baseNamespace, String localPart) throws ProcessingException { try { DocumentBuilderFactory factory = getDocumentBuilderFactory(); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.getDOMImplementation().createDocument(baseNamespace, localPart, null); } catch (DOMException e) { throw logger.processingError(e); } catch (ParserConfigurationException e) { throw logger.processingError(e); } }
public static Document getDocumentFromSource(Source source) throws ProcessingException, ConfigurationException { try { Transformer transformer = TransformerUtil.getTransformer(); DOMResult result = new DOMResult(); TransformerUtil.transform(transformer, source, result); return (Document) result.getNode(); } catch (ParsingException te) { throw logger.processingError(te); } }
/** * Get the {@link Source} as an {@link InputStream} * * @param source * @return * @throws ConfigurationException * @throws ProcessingException */ public static InputStream getSourceAsStream(Source source) throws ConfigurationException, ProcessingException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Result streamResult = new StreamResult(baos); // Write the DOM document to the stream Transformer transformer = TransformerUtil.getTransformer(); try { transformer.transform(source, streamResult); } catch (TransformerException e) { throw logger.processingError(e); } return new ByteArrayInputStream(baos.toByteArray()); }
/** * Get Document from an inputstream * * @param is * @return * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ public static Document getDocument(InputStream is) throws ConfigurationException, ProcessingException, ParsingException { DocumentBuilderFactory factory = getDocumentBuilderFactory(); try { DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(is); } catch (ParserConfigurationException e) { throw logger.configurationError(e); } catch (SAXException e) { throw logger.parserError(e); } catch (IOException e) { throw logger.processingError(e); } }
/** * Marshall a DOM Element as string * * @param element * @return * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static String getDOMElementAsString(Element element) throws ProcessingException, ConfigurationException { Source source = new DOMSource(element); StringWriter sw = new StringWriter(); Result streamResult = new StreamResult(sw); // Write the DOM document to the file Transformer xformer = TransformerUtil.getTransformer(); try { xformer.transform(source, streamResult); } catch (TransformerException e) { throw logger.processingError(e); } return sw.toString(); }