LSParser parser = impl.createLSParser( DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/TR/REC-xml");
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); }
protected Object createObject() throws Exception { LSParser parser = domImplementationLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); return parser; } }
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS"); LSParser lsParser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); Document document = lsParser.parseURI("myFile.xml");
/** * Creates basic LSParser instance and sets common * properties and configuration parameters. * @return */ public LSParser createLSParser() { LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); configureParser(parser); return parser; }
// we need to get the DOMImplementation from the Document - if header is an // Element then do: DOMImplementationLS ls = (DOMImplementationLS)header.getOwnerDocument().getImplementation(); // if header is already a Document then it's just //DOMImplementationLS ls = // (DOMImplementationLS)header.getImplementation(); // LSInput represents the source from which the XML to be parsed will be taken LSInput input = ls.createLSInput(); input.setStringData(Titleproper); // LSParser does the parsing LSParser parser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); // parseWithContext(input, context, action) parses the fragment given by input // and inserts the results at a position relative to the node "context". In this // case we use ACTION_REPLACE_CHILDREN which means remove all the child nodes // (if any) from the context node and replace them with the result of the parse. // Alternative actions include replacing the context node entirely, inserting // the parse results as siblings before or after the context node, etc. parser.parseWithContext(input, header.getElementsByTagName("titleproper").item(0), LSParser.ACTION_REPLACE_CHILDREN);
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);
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS lsImpl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSParser requestConfigParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
/** * 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 ); }
/** * 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; } }
/** * 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; } }
LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); if (resolver != null) { parser.getDomConfig().setParameter("resource-resolver", resolver);
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); }
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); }
LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); if (resolver != null) { parser.getDomConfig().setParameter("resource-resolver", resolver);
/** * 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 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 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); } }
/** * 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); }