List of usage examples for org.w3c.dom.bootstrap DOMImplementationRegistry getDOMImplementation
public DOMImplementation getDOMImplementation(final String features)
null
if none is found. From source file:Main.java
public static String lsSerializeDom(Node doc) throws Exception { if (doc == null) return null; DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); return writer.writeToString(doc); }
From source file:Main.java
public static final String prettyPrint(final Document aNode) throws Exception { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impls = (DOMImplementationLS) registry.getDOMImplementation("LS"); // Prepare the output LSOutput domOutput = impls.createLSOutput(); domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name()); StringWriter writer = new StringWriter(); domOutput.setCharacterStream(writer); LSSerializer domWriter = impls.createLSSerializer(); DOMConfiguration domConfig = domWriter.getDomConfig(); domConfig.setParameter("format-pretty-print", true); domConfig.setParameter("element-content-whitespace", true); domWriter.setNewLine("\r\n"); domConfig.setParameter("cdata-sections", Boolean.TRUE); // And finaly, write domWriter.write(aNode, domOutput);/*from w w w . jav a 2 s. c om*/ return domOutput.getCharacterStream().toString(); }
From source file:Main.java
public static <T extends OutputStream> T lsSerializeDom(Node doc, T byteStream, String encoding) throws Exception { if (doc == null) return byteStream; DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSOutput lsOutput = impl.createLSOutput(); lsOutput.setByteStream(byteStream);/*from w w w.j ava2s. co m*/ encoding = encoding == null ? "UTF-8" : encoding; lsOutput.setEncoding(encoding); impl.createLSSerializer().write(doc, lsOutput); return byteStream; }
From source file:javatojs.DomUtil.java
public static void writeDocument(Document document) { try {//from w ww .j a va 2 s . co m DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); String documentStr = writer.writeToString(document); System.out.println("Serialized document: \n" + documentStr); } catch (Exception e) { System.out.println("ERROR writing document: \n "); e.printStackTrace(); } }
From source file:Main.java
public static String prettyFormat(String input) { try {//from w w w . ja va 2s . c om final InputSource src = new InputSource(new StringReader(input)); final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) .getDocumentElement(); final Boolean keepDeclaration = Boolean.valueOf(input.startsWith("<?xml")); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified. writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted. return writer.writeToString(document); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:javatojs.DomUtil.java
public static Document readDocument(String uri) { Document document = null;//from w w w. ja v a 2 s . c om try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); System.out.println("In readDocument uri: " + uri); document = builder.parseURI(uri); if (document == null) { System.out.println("Null doc returned!!!!!!!!"); } System.out.println("Read toc: \n " + document); } catch (Exception e) { System.out.println("ERROR reading toc: \n "); e.printStackTrace(); } return document; }
From source file:Main.java
/** * /*w w w. ja va 2 s. com*/ * @param xmlNode * @return */ public static String serializeNode(Node xmlNode) { try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); LSOutput output = impl.createLSOutput(); output.setByteStream(buffer); writer.write(xmlNode, output); return new String(buffer.toByteArray()); } catch (Exception e) { /* Serialize node is for debuging only */ return null; } }
From source file:Main.java
/** * 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/*from w w w . j av a 2s . co m*/ * * @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); }
From source file:Main.java
/** * Save an XML file.//from w w w .j a va 2 s.c om */ public static void saveXML(Document document, File file) { try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); FileOutputStream fos = new FileOutputStream(file); LSOutput lso = impl.createLSOutput(); lso.setByteStream(fos); writer.write(document, lso); /* OutputFormat of = new OutputFormat(document, "ISO-8859-1", true); serializer.setOutputFormat(of); serializer.setOutputCharStream(new FileWriter(file)); serializer.serialize(document); */ } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String formatXml(String xml) { try {/*from w ww . j a va 2 s . c om*/ final InputSource src = new InputSource(new StringReader(xml)); final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) .getDocumentElement(); final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml")); //May need this: System.setProperty(DOMImplementationRegistry. //PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); // Set this to true if the output needs to be beautified. writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the declaration is needed to be outputted. writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); return writer.writeToString(document); } catch (Exception e) { System.err.println("Exception thrown"); throw new RuntimeException(e); } }