List of usage examples for org.dom4j.io OutputFormat setSuppressDeclaration
public void setSuppressDeclaration(boolean suppressDeclaration)
This will set whether the XML declaration (<?xml version="1.0" encoding="UTF-8"?>
) is included or not.
From source file:W3cDocument.java
License:Open Source License
public static String getXml(org.w3c.dom.Document w3cDoc, String encoding) { try {//from w w w. ja va 2 s . c om org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader(); org.dom4j.Document dom4jDoc = xmlReader.read(w3cDoc); //? OutputFormat format = new OutputFormat();//(" ", true); //? format.setEncoding(encoding); //format.setOmitEncoding(true); format.setSuppressDeclaration(true); //xml StringWriter out = new StringWriter(); XMLWriter xmlWriter = new XMLWriter(out, format); xmlWriter.setEscapeText(true); //?doc xmlWriter.write(dom4jDoc); xmlWriter.flush(); //??printWriter String xml = out.toString(); out.close(); return xml; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:au.com.acegi.xmlformat.XmlFormatPlugin.java
License:Apache License
private OutputFormat buildFormatter() { final OutputFormat fmt = createPrettyPrint(); fmt.setAttributeQuoteCharacter(attributeQuoteChar); fmt.setEncoding(encoding);//from w ww. ja va2s. co m fmt.setExpandEmptyElements(expandEmptyElements); fmt.setIndentSize(indentSize); fmt.setLineSeparator(determineLineSeparator()); fmt.setNewLineAfterDeclaration(newLineAfterDeclaration); fmt.setNewLineAfterNTags(newLineAfterNTags); fmt.setNewlines(newlines); fmt.setOmitEncoding(omitEncoding); fmt.setPadText(padText); fmt.setSuppressDeclaration(suppressDeclaration); fmt.setTrimText(trimText); fmt.setXHTML(xhtml); return fmt; }
From source file:be.hikage.maven.plugin.xmlmerge.MergeXmlMojo.java
License:Apache License
private void writeMergedXml(File baseFile, Document base, StringBuilder prologHeader) throws IOException { FileOutputStream fos = new FileOutputStream(baseFile); if (processProlog && prologHeader != null && StringUtils.isNotEmpty(prologHeader.toString())) { fos.write(prologHeader.toString().getBytes()); }/*from w ww. j a va 2 s . c o m*/ OutputFormat format = OutputFormat.createPrettyPrint(); format.setSuppressDeclaration(true); format.setNewLineAfterDeclaration(false); XMLWriter writer = new XMLWriter(fos, format); writer.write(base); writer.flush(); writer.close(); }
From source file:com.amalto.workbench.utils.Util.java
License:Open Source License
public static String formatXsdSource(String xsdSource, boolean suppressDeclaration) { try {/*from w ww .j a v a2 s . c om*/ SAXReader reader = new SAXReader(); org.dom4j.Document document = reader.read(new StringReader(xsdSource)); StringWriter writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8");//$NON-NLS-1$ format.setSuppressDeclaration(suppressDeclaration); XMLWriter xmlwriter = new XMLWriter(writer, format); xmlwriter.write(document); String str = writer.toString(); writer.close(); xmlwriter.close(); return str; } catch (Exception e) { log.error(e.getMessage(), e); } return xsdSource; }
From source file:com.amalto.workbench.utils.XmlUtil.java
License:Open Source License
public static String formatXmlSource(String xmlSource) { SAXReader reader = new SAXReader(); StringReader stringReader = new StringReader(xmlSource); StringWriter writer = null;/*from ww w.j a va 2s. c om*/ XMLWriter xmlwriter = null; String result = xmlSource; try { Document document = reader.read(stringReader); writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8");//$NON-NLS-1$ format.setIndentSize(4); format.setSuppressDeclaration(true); xmlwriter = new XMLWriter(writer, format); xmlwriter.write(document); result = writer.toString(); } catch (Exception e) { } finally { try { if (stringReader != null) stringReader.close(); if (xmlwriter != null) xmlwriter.close(); if (writer != null) writer.close(); } catch (Exception e) { } } return result; }
From source file:com.feilong.tools.dom4j.Dom4jUtil.java
License:Apache License
/** * ? xml.<br>//from w w w . j a v a 2s . co m * * <pre> * {@code * <wddxPacket version='1.0'><header/><data><struct><var name='TRANSACTIONID'><string></string></var><var name='ACQUIRERRESPONSECODE'><string></string></var><var name='SCRUBMESSAGE'><string>Invalid siteID</string></var><var name='AMOUNT'><string></string></var><var name='SERVICEVERSION'><string>2.0</string></var><var name='TRANSACTIONSCRUBCODE'><string></string></var><var name='MERCHANTTRANSACTIONID'><string>010003170001</string></var><var name='CURRENCY'><string></string></var><var name='TRANSACTIONSTATUS'><string></string></var><var name='SITEID'><string>Blanja2</string></var><var name='TRANSACTIONDATE'><string></string></var><var name='ACQUIRERCODE'><string></string></var><var name='SCRUBCODE'><string>50007</string></var><var name='TRANSACTIONSCRUBMESSAGE'><string></string></var><var name='ACQUIRERAPPROVALCODE'><string></string></var><var name='TRANSACTIONTYPE'><string>AUTHORIZATION</string></var></struct></data></wddxPacket> * } * </pre> * * ? * * <pre> * {@code * <wddxPacket version="1.0"> * <header/> * <data> * <struct> * <var name="TRANSACTIONID"> * <string/> * </var> * <var name="ACQUIRERRESPONSECODE"> * <string/> * </var> * <var name="SCRUBMESSAGE"> * <string>Invalid siteID</string> * </var> * <var name="AMOUNT"> * <string/> * </var> * <var name="SERVICEVERSION"> * <string>2.0</string> * </var> * <var name="TRANSACTIONSCRUBCODE"> * <string/> * </var> * <var name="MERCHANTTRANSACTIONID"> * <string>010003170001</string> * </var> * <var name="CURRENCY"> * <string/> * </var> * <var name="TRANSACTIONSTATUS"> * <string/> * </var> * <var name="SITEID"> * <string>Blanja2</string> * </var> * <var name="TRANSACTIONDATE"> * <string/> * </var> * <var name="ACQUIRERCODE"> * <string/> * </var> * <var name="SCRUBCODE"> * <string>50007</string> * </var> * <var name="TRANSACTIONSCRUBMESSAGE"> * <string/> * </var> * <var name="ACQUIRERAPPROVALCODE"> * <string/> * </var> * <var name="TRANSACTIONTYPE"> * <string>AUTHORIZATION</string> * </var> * </struct> * </data> * </wddxPacket> * * } * </pre> * * @param xmlString * the xml string * @param encoding * the encoding * @param writer * the writer * @return the string * @throws Dom4jException * the dom4j exception * @since 1.0.8 */ public static String format(String xmlString, String encoding, Writer writer) throws Dom4jException { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); // XML? outputFormat.setEncoding(encoding); // XML? outputFormat.setSuppressDeclaration(true); outputFormat.setIndent(true); // ? outputFormat.setIndent(" "); // tab? outputFormat.setNewlines(true); // ?? XMLWriter xmlWriter = new XMLWriter(writer, outputFormat); xmlWriter.setEscapeText(false); Document document = string2Document(xmlString); try { xmlWriter.write(document); xmlWriter.close(); return writer.toString(); } catch (IOException e) { throw new Dom4jException(e); } }
From source file:com.googlecode.fascinator.common.sax.SafeSAXReader.java
License:Open Source License
/** * Convert node to string// w w w. j a v a 2 s . c om * * @param outDoc Node to be converted * @return String of the converted node * @throws IOException if the conversion fail */ public String docToString(Node outDoc) throws IOException { Writer osw = new StringWriter(); OutputFormat opf = new OutputFormat("", false, "UTF-8"); opf.setSuppressDeclaration(true); opf.setExpandEmptyElements(true); XMLWriter writer = new XMLWriter(osw, opf); writer.setEscapeText(false); writer.write(outDoc); writer.close(); return osw.toString(); }
From source file:com.googlecode.fascinator.common.sax.SafeSAXReader.java
License:Open Source License
/** * Convert node to stream//from w w w .j a v a 2s.c o m * * @param outDoc Node to be converted * @param outStream output stream of the converted node * @throws IOException if the conversion fail */ public void docToStream(Node outDoc, OutputStream outStream) throws IOException { OutputFormat opf = new OutputFormat("", false, "UTF-8"); opf.setSuppressDeclaration(true); opf.setExpandEmptyElements(true); XMLWriter writer = new XMLWriter(outStream, opf); writer.setEscapeText(false); writer.write(outDoc); writer.close(); }
From source file:com.liveneo.plat.web.action.MakeLicfile.java
public static void createXmlFile(String fileName, Document doc) { try {/* ww w . java 2 s.c om*/ File testfile = new File("fileName"); if (testfile.exists()) { testfile.delete(); } FileWriter fileWriter = new FileWriter(fileName); OutputFormat xmlFormat = OutputFormat.createPrettyPrint(); xmlFormat.setEncoding("UTF-8"); xmlFormat.setSuppressDeclaration(false); xmlFormat.setExpandEmptyElements(false); XMLWriter xmlWriter = new XMLWriter(fileWriter, xmlFormat); xmlWriter.write(doc); xmlWriter.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.rockagen.commons.util.XmlUtil.java
License:Apache License
/** * Format xml {@link OutputFormat#createPrettyPrint()} * * @param xmlStr xml String * @param enc encoding * @param isSuppressDeclaration set supperss declaration * @return pretty String//from w w w. ja v a 2s.co m */ public static String formatPretty(String xmlStr, String enc, boolean isSuppressDeclaration) { if (CommUtil.isBlank(xmlStr)) return xmlStr; if (enc == null) enc = ENCODING; OutputFormat formater = OutputFormat.createPrettyPrint(); formater.setEncoding(enc); formater.setSuppressDeclaration(isSuppressDeclaration); return format(xmlStr, formater); }