Directamente en el código:
Importar parte:
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.File;
import java.io.FileOutputStream;
public static void xmlWriterWithFormat (Documento doc, Archivo xmlFile) arroja Exception { XMLWriter xmlWriter = null; OutputFormat format = OutputFormat.createPrettyPrint (); // Establecer formato de salida de documento XML format.setEncoding ("UTF-8"); // Establecer XML El tipo de codificación del documento format.setIndent (true); // Establecer si se aplicará sangría a format.setIndent (""); // Realizar sangría con espacios format.setNewlines (true); // Establecer si se debe ajustar try { xmlWriter = new XMLWriter (nuevo FileOutputStream (xmlFile), formato); xmlWriter.write (doc); } catch (Exception e) { lanzar nueva Exception (e); } finalmente { if (xmlWriter! = null) {
xmlWriter.close ();
}
}
}