/** * Returns a new Format object that performs whitespace beautification with * 2-space indents, uses the UTF-8 encoding, doesn't expand empty elements, * includes the declaration and encoding, and uses the default entity * escape strategy. * Tweaks can be made to the returned Format instance without affecting * other instances. * * @return a Format with whitespace beautification */ public static Format getPrettyFormat() { Format f = new Format(); f.setIndent(STANDARD_INDENT); f.setTextMode(TextMode.TRIM); return f; }
private void doXmlOutputter(Document doc, File xmlFile) throws IOException { Format format = Format.getPrettyFormat(); format.setIndent(" "); format.setEncoding("UTF-8"); XMLOutputter out = new XMLOutputter(format); out.output(doc, new FileWriter(xmlFile)); // 写文件 }
/** * There are dummy attributes in the root element of the XSL, which are removed here. * Those attributes were generated during transformation to force passing through namespace nodes. */ private MCRContent removeDummyAttributes(MCRContent xsl) throws JDOMException, IOException, SAXException { Document doc = xsl.asXML(); List<Attribute> attributes = doc.getRootElement().getAttributes(); for (Iterator<Attribute> iter = attributes.iterator(); iter.hasNext();) { if ("dummy".equals(iter.next().getName())) { iter.remove(); } } MCRJDOMContent out = new MCRJDOMContent(doc); out.setFormat(Format.getPrettyFormat().setIndent(" ")); return out; } }
/** * Writes the specified JDOM element to the XML file specified by filename. * The file will use UTF-8 encoding and has 4-space indentation. * * @param eRoot root element of the XML document * @param filename filename of the XML file to create * @throws IOException */ public static void writeXMLFile(Element eRoot, String filename) throws IOException { Document document = new Document(eRoot); XMLOutputter outputter = new XMLOutputter(); Format format = Format.getPrettyFormat(); format.setLineSeparator(System.getProperty("line.separator")); format.setIndent(" "); outputter.setFormat(format); // FileWriter can't be used here, because default encoding on Win32 isn't UTF-8 try (OutputStreamWriter osWriter = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8")) { outputter.output(document, osWriter); osWriter.flush(); } } }
fmt.setIndent(" "); // 2 spaces
private void writeXmlWithExcludes(InputStream inputStream, OutputStream outputStream, Set<String> namespacePrefixes) throws IOException, JDOMException { SAXBuilder saxBuilder = new SAXBuilder(); Document doc = saxBuilder.build(inputStream); Set<String> namespacePrefixesActuallyUsed = new HashSet<>(); applyXmlExcludes(doc.getRootElement(), "", namespacePrefixesActuallyUsed); XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat() .setIndent(" ") .setLineSeparator(LineSeparator.UNIX)); outputter.setXMLOutputProcessor(new OneAttributePerLineXmlProcessor(namespacePrefixes, namespacePrefixesActuallyUsed)); outputter.output(doc, outputStream); outputStream.flush(); }
/** * Writes the internal xml after validation to file. * * @param localDoc * the local doc * @param outFilename * the output file name */ public static void writeInternalXmlToFile(Document localDoc, String outFilename) { final PrintWriter writer = FileUtils.getWriter(outFilename); final XMLOutputter outputter = new XMLOutputter(); Format format = Format.getPrettyFormat(); format.setIndent(" "); format.setLineSeparator("\n"); outputter.setFormat(format); outputter.setFormat(format); try { LOG.info(" write internal xml after validation to file \"" + outFilename + "\""); outputter.output(localDoc, writer); } catch (final Exception e) { e.printStackTrace(); } }
/** * Method write * * @param project * @param stream * @param document * @deprecated */ public void write( Model project, Document document, OutputStream stream ) throws java.io.IOException { updateModel( project, "project", new Counter( 0 ), document.getRootElement() ); XMLOutputter outputter = new XMLOutputter(); Format format = Format.getPrettyFormat(); format.setIndent( " " ).setLineSeparator( System.getProperty( "line.separator" ) ); outputter.setFormat( format ); outputter.output( document, stream ); } // -- void write(Model, Document, OutputStream)
format.setExpandEmptyElements( false ); format.setOmitDeclaration( false ); format.setIndent( "\t" ); format.setLineSeparator( LineSeparator.CRNL );
format.setIndent( "\t" ); format.setLineSeparator( LineSeparator.CRNL );