List of usage examples for org.w3c.dom.ls DOMImplementationLS createLSSerializer
public LSSerializer createLSSerializer();
LSSerializer
object. From source file:javatojs.DomUtil.java
public static void writeDocument(Document document) { try {/*from w w w. j a v a 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 XMLtoString(Node node) throws Exception { Document document = node.getOwnerDocument(); DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); serializer.getDomConfig().setParameter("xml-declaration", false); return serializer.writeToString(node); }
From source file:Main.java
public static String prettyFormat(String input) { try {//from w ww . java2 s . 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:Main.java
public static void writeDocument(Document doc, Writer out) throws IOException { // happy to replace this code w/ the non-deprecated code, but I couldn't get the transformer // approach to work. // OutputFormat format = new OutputFormat(doc); // format.setIndenting(true); // format.setIndent(2); // XMLSerializer serializer = new XMLSerializer(out, format); // serializer.serialize(doc); DOMImplementationLS impl = (DOMImplementationLS) doc.getImplementation(); LSSerializer writer = impl.createLSSerializer(); DOMConfiguration config = writer.getDomConfig(); if (config.canSetParameter("format-pretty-print", Boolean.TRUE)) { config.setParameter("format-pretty-print", Boolean.TRUE); }//from w w w . ja v a 2s.c om // what a crappy way to force the stream to be UTF-8. yuck! ByteArrayOutputStream baos = new ByteArrayOutputStream(); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); output.setByteStream(baos); writer.write(doc, output); out.write(baos.toString()); out.flush(); }
From source file:Main.java
/** * //from w w w. java 2s . co m * @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
static public String getInnerXmlText(Node xmlNode) { StringBuilder result = new StringBuilder(); Document xmlDocument = xmlNode.getOwnerDocument(); DOMImplementation xmlDocumentImpl = xmlDocument.getImplementation(); DOMImplementationLS lsImpl = (DOMImplementationLS) xmlDocumentImpl.getFeature("LS", "3.0"); LSSerializer lsSerializer = lsImpl.createLSSerializer(); NodeList childNodes = xmlNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { String childText = lsSerializer.writeToString(childNodes.item(i)); int pos = childText.indexOf("?>"); if (pos > -1) { childText = childText.substring(pos + 2); }/*ww w. ja v a 2 s. c om*/ result.append(childText); } return result.toString(); }
From source file:Main.java
public static String toNormalizedXML(InputStream is) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);// w ww .j a va 2s. c o m dbf.setCoalescing(true); dbf.setIgnoringElementContentWhitespace(true); dbf.setIgnoringComments(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(is); document.normalizeDocument(); document.getDocumentElement().setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", //$NON-NLS-1$ "xsi:schemaLocation", //$NON-NLS-1$ "http://abc4trust.eu/wp2/abcschemav1.0 ../../../../../../../../../abc4trust-xml/src/main/resources/xsd/schema.xsd"); //$NON-NLS-1$ DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); String xml = serializer.writeToString(document); return trim(xml); }
From source file:Main.java
/** * Save an XML file.//from w ww . jav a 2s. c o m */ 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 v a 2 s. c o m*/ 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); } }
From source file:Main.java
public static String serializeNode(Node node) throws LSException, IllegalAccessException, DOMException, InstantiationException, ClassNotFoundException, ClassCastException { String serializedElement = null; if (node != null) { System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMImplementationSourceImpl"); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); serializedElement = writer.writeToString(node); }/*from w w w.j av a2s. c om*/ return serializedElement; }