List of usage examples for org.w3c.dom.ls LSSerializer writeToString
public String writeToString(Node nodeArg) throws DOMException, LSException;
LSSerializer
interface. From source file:com.qualogy.qafe.bind.orm.jibx.ORMBinder.java
public static String docToString(Document doc) { DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); return lsSerializer.writeToString(doc); }
From source file:Main.java
public static String format(String xml) throws Exception { InputSource src = new InputSource(new StringReader(xml)); Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement(); Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml")); System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); return writer.writeToString(document); }
From source file:Main.java
static public final String toString(Node node, boolean declaration) { try {/*from w w w . j av a 2 s . co m*/ DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("xml-declaration", declaration); return writer.writeToString(node); } catch (Exception e) { return e.toString(); } }
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); }/* w w w . j ava2 s .c o m*/ result.append(childText); } return result.toString(); }
From source file:Main.java
/** * * @param rootElement//from ww w . j a v a2 s. co m * @param setXmlDecl * @return */ public static String serializeToString(Element rootElement, boolean setXmlDecl) { DOMImplementationLS lsImpl = (DOMImplementationLS) rootElement.getOwnerDocument().getImplementation() .getFeature("LS", "3.0"); LSSerializer serializer = lsImpl.createLSSerializer(); serializer.getDomConfig().setParameter("xml-declaration", setXmlDecl); // set it to false to get // String without // xml-declaration return serializer.writeToString(rootElement); }
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// w w w . j a v a2s. c om * * @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
/** * Converts a Document to a String//from w w w . j a v a2 s . c o m * * @param doc * The Document to be converted * @return The String representation of the Document */ public static String convertDocumentToString(final Document doc) { final DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); final LSSerializer lsSerializer = domImplementation.createLSSerializer(); // lsSerializer.getDomConfig().setParameter("format-pretty-print", // Boolean.TRUE); final String xml = lsSerializer.writeToString(doc); return xml; }
From source file:no.kantega.commons.util.XMLHelper.java
public static String getString(Document doc) throws SystemException { DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); return lsSerializer.writeToString(doc); }
From source file:Main.java
private static String parseXmlDocToString(Document xmlDoc) throws ClassNotFoundException, InstantiationException, IllegalAccessException { // Add formatting to the xml document in case we want to save to a file 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(); writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified. writer.getDomConfig().setParameter("xml-declaration", true); // Set this to true if the declaration is needed to be outputted return writer.writeToString(xmlDoc); }
From source file:Main.java
public static String prettyFormat(String input) { try {/* ww w . ja va 2s . c o m*/ 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); } }