private Document getDocument(InputStream is) throws InstantiationException, IllegalAccessException, ClassNotFoundException { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS"); LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); // we need to ignore whitespace here so the nodeToString() method will be able to indent it properly: parser.setFilter(new IgnoreWhitespaceFilter()); LSInput domInput = impl.createLSInput(); domInput.setByteStream(is); return parser.parse(domInput); }
private static Document parse(LSInput input, boolean validateIfSchema) { DOMImplementationLS impl = getDOMImpl(); LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); DOMConfiguration config = parser.getDomConfig(); config.setParameter("element-content-whitespace", false); config.setParameter("namespaces", true); config.setParameter("validate-if-schema", validateIfSchema); return parser.parse(input); }
/** * Parse message payload with DOM implementation. * @param messagePayload * @throws CitrusRuntimeException * @return DOM document. */ public static Document parseMessagePayload(String messagePayload) { LSParser parser = configurer.createLSParser(); LSInput receivedInput = configurer.createLSInput(); try { Charset charset = getTargetCharset(messagePayload); receivedInput.setByteStream(new ByteArrayInputStream(messagePayload.trim().getBytes(charset))); receivedInput.setEncoding(charset.displayName()); } catch(UnsupportedEncodingException e) { throw new CitrusRuntimeException(e); } return parser.parse(receivedInput); }
DOMImplementationLS ls = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS"); // Gets a basic document from string. LSInput input = ls.createLSInput(); String xml = "<bookstore city='shanghai'><a></a><b/></bookstore>"; InputStream istream = new ByteArrayInputStream(xml.getBytes("UTF-8")); input.setByteStream(istream); LSParser parser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); Document document = parser.parse(input); // Creates a LSSerializer object and saves to file. LSSerializer serializer = ls.createLSSerializer(); serializer.getDomConfig().setParameter("format-pretty-print", true); LSOutput output = ls.createLSOutput(); OutputStream ostream = new FileOutputStream("c:\\temp\\foo.xml"); output.setByteStream(ostream); serializer.write(document, output);
LSInput lsInput = lsImpl.createLSInput(); lsInput.setStringData(content); Document commitRequestDoc = requestConfigParser.parse(lsInput); Node rootNode = commitRequestDoc.getFirstChild(); if (!"CommitRequest".equals(rootNode.getNodeName()))
/** * Parse some XML into a Documentusing the Document Object Model (DOM) Level 3 * Load and Save Implementation * @param xml XML as a String * @return Document */ public static final Document parseXmlLS( final String xml ) { init(); final LSInput in = domImplLs.createLSInput(); in.setStringData( xml ); final LSParser parser = domImplLs.createLSParser( DOMImplementationLS.MODE_SYNCHRONOUS, null ); return parser.parse( in ); }
/** * Pretty prints a XML string. * @param xml * @throws CitrusRuntimeException * @return pretty printed XML string */ public static String prettyPrint(String xml) { LSParser parser = configurer.createLSParser(); configurer.setParserConfigParameter(parser, VALIDATE_IF_SCHEMA, false); LSInput input = configurer.createLSInput(); try { Charset charset = getTargetCharset(xml); input.setByteStream(new ByteArrayInputStream(xml.trim().getBytes(charset))); input.setEncoding(charset.displayName()); } catch(UnsupportedEncodingException e) { throw new CitrusRuntimeException(e); } Document doc; try { doc = parser.parse(input); } catch (Exception e) { return xml; } return serialize(doc); }
/** * Parse a DOM Document from a source. * * @param source the source input to parse * @return the equivalent DOM Document, or {@code null} if the parsing failed. */ public static Document parse(LSInput source) { try { LSParser p = LS_IMPL.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); // Disable validation, since this takes a lot of time and causes unneeded network traffic p.getDomConfig().setParameter("validate", false); if (p.getDomConfig().canSetParameter(DISABLE_DTD_PARAM, false)) { p.getDomConfig().setParameter(DISABLE_DTD_PARAM, false); } return p.parse(source); } catch (Exception ex) { LOGGER.warn("Cannot parse XML document: [{}]", ex.getMessage()); return null; } }
/** * Parse a DOM Document from a source. * * @param source the source input to parse * @return the equivalent DOM Document, or {@code null} if the parsing failed. */ public Document parse(LSInput source) { try { LSParser p = this.lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); // Disable validation, since this takes a lot of time and causes unneeded network traffic p.getDomConfig().setParameter("validate", false); if (p.getDomConfig().canSetParameter(DISABLE_DTD_PARAM, false)) { p.getDomConfig().setParameter(DISABLE_DTD_PARAM, false); } return p.parse(source); } catch (Exception ex) { LOG.warn("Cannot parse XML document: " + ex.getMessage()); return null; } }
private static Document parse(LSInput input, boolean validateIfSchema) { DOMImplementationLS impl = getDOMImpl(); LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); DOMConfiguration config = parser.getDomConfig(); config.setParameter("element-content-whitespace", false); config.setParameter("namespaces", true); config.setParameter("validate-if-schema", validateIfSchema); return parser.parse(input); }
domInput.setByteStream(content); this.content = parser.parse(domInput); content.close(); } catch (IOException e) {
private static Document parse(LSInput input, boolean validateIfSchema) { DOMImplementationLS impl = getDOMImpl(); LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); DOMConfiguration config = parser.getDomConfig(); config.setParameter("element-content-whitespace", false); config.setParameter("namespaces", true); config.setParameter("validate-if-schema", validateIfSchema); return parser.parse(input); }
domInput.setByteStream(content); this.content = parser.parse(domInput); } catch (ParserConfigurationException e) { logger.error("Failed to parse DOM document from input stream",e);
/** * Normalize and pretty-print XML so that it can be compared using string * compare. The following code does the following: - Removes comments - * Makes sure attributes are ordered consistently - Trims every element - * Pretty print the document * * @param xml The XML to be normalized * @return The equivalent XML, but now normalized */ public static String normalizeXML(String xml) throws Exception { // Remove all white space adjoining tags ("trim all elements") xml = xml.replaceAll("\\s*<", "<"); xml = xml.replaceAll(">\\s*", ">"); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); LSInput input = domLS.createLSInput(); input.setStringData(xml); Document document = lsParser.parse(input); LSSerializer lsSerializer = domLS.createLSSerializer(); lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE); lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); return lsSerializer.writeToString(document); }
private static DOMSource read(LSInput aInput, LSResourceResolver aResolver) throws XMLException { try { DOMImplementationLS impl = DomUtils.getDomImplementationLS(); LSParser builder = impl.createLSParser( DOMImplementationLS.MODE_SYNCHRONOUS, null); if (aResolver != null) { builder.getDomConfig().setParameter("resource-resolver", aResolver); } Document docraw = builder.parse(aInput); DOMSource result = new DOMSource(docraw); result.setSystemId(aInput.getSystemId()); return result; } catch (LSException e) { throw new XMLException(e.getMessage(), e); } }
private Document getDocument(InputStream is) throws InstantiationException, IllegalAccessException, ClassNotFoundException { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS"); LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); // we need to ignore whitespace here so the nodeToString() method will be able to indent it properly: parser.setFilter(new IgnoreWhitespaceFilter()); LSInput domInput = impl.createLSInput(); domInput.setByteStream(is); return parser.parse(domInput); }
public Document parse(InputStream is, boolean ignoreBlanks) throws XMLException { try { LSParser parser = getParser(); try { if(ignoreBlanks) { parser.setFilter(BLANK_FILTER); } LSInput ip = domImplementationLS.createLSInput(); ip.setByteStream(is); Document doc = parser.parse(ip); return doc; } finally { recycleParser(parser); } } catch(Exception e) { throw new XMLException(e,"Error parsing XML input stream"); // $NLS-XercesDriverL3.ErrorwhileparsingXMLinputstream-1$ } }
public Document parse(Reader reader, boolean ignoreBlanks) throws XMLException { try { LSParser parser = getParser(); try { if(ignoreBlanks) { parser.setFilter(BLANK_FILTER); } LSInput ip = domImplementationLS.createLSInput(); ip.setCharacterStream(reader); Document doc = parser.parse(ip); return doc; } finally { recycleParser(parser); } } catch(Exception e) { throw new XMLException(e,"Error parsing XML character stream"); // $NLS-XercesDriverL3.ErrorwhileparsingXMLchracterstrea-1$ } }
/** * Normalize and pretty-print XML so that it can be compared using string * compare. The following code does the following: - Removes comments - * Makes sure attributes are ordered consistently - Trims every element - * Pretty print the document * * @param xml The XML to be normalized * @return The equivalent XML, but now normalized */ public static String normalizeXML(String xml) throws Exception { // Remove all white space adjoining tags ("trim all elements") xml = xml.replaceAll("\\s*<", "<"); xml = xml.replaceAll(">\\s*", ">"); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); LSInput input = domLS.createLSInput(); input.setStringData(xml); Document document = lsParser.parse(input); LSSerializer lsSerializer = domLS.createLSSerializer(); lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE); lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); return lsSerializer.writeToString(document); }