Java Code Examples for org.dom4j.io.OutputFormat.setIndent()

The following are Jave code examples for showing how to use setIndent() of the org.dom4j.io.OutputFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: gitplex-mit   File: VersionedDocument.java   Source Code and License Vote up 6 votes
public String toXML(boolean pretty) {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	try {
		OutputFormat format = new OutputFormat();
		format.setEncoding(Charsets.UTF_8.name());
		if (pretty) {
			format.setIndent(true);
	        format.setNewlines(true);
		} else {
	        format.setIndent(false);
	        format.setNewlines(false);
		}
		new XMLWriter(baos, format).write(getWrapped());
		return baos.toString(Charsets.UTF_8.name());
	} catch (Exception e) {
		throw Throwables.propagate(e);
	}
}
 
Example 2
Project: CEC-Automatic-Annotation   File: WriteToXMLUtil.java   Source Code and License Vote up 6 votes
public static boolean writeToXML(Document document, String tempPath) {
	try {
		// 使用XMLWriter写入,可以控制格式,经过调试,发现这种方式会出现乱码,主要是因为Eclipse中xml文件和JFrame中文件编码不一致造成的
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding(EncodingUtil.CHARSET_UTF8);
		// format.setSuppressDeclaration(true);//这句话会压制xml文件的声明,如果为true,就不打印出声明语句
		format.setIndent(true);// 设置缩进
		format.setIndent("	");// 空行方式缩进
		format.setNewlines(true);// 设置换行
		XMLWriter writer = new XMLWriter(new FileWriterWithEncoding(new File(tempPath), EncodingUtil.CHARSET_UTF8), format);
		writer.write(document);
		writer.close();
	} catch (IOException e) {
		e.printStackTrace();
		MyLogger.logger.error("写入xml文件出错!");
		return false;
	}
	return true;
}
 
Example 3
Project: EasyML   File: WFGraph.java   Source Code and License Vote up 5 votes
public static void main(String args[]){

		Namespace rootNs = new Namespace("", "uri:oozie:workflow:0.4"); // root namespace uri
		QName rootQName = QName.get("workflow-app", rootNs); // your root element's name
		Element workflow = DocumentHelper.createElement(rootQName);
		Document doc = DocumentHelper.createDocument(workflow);
		
		workflow.addAttribute("name", "test");
		Element test = workflow.addElement("test");
		test.addText("hello");
				OutputFormat outputFormat = OutputFormat.createPrettyPrint();
				outputFormat.setEncoding("UTF-8");
				outputFormat.setIndent(true); 
				outputFormat.setIndent("    "); 
				outputFormat.setNewlines(true); 
		try {
			StringWriter stringWriter = new StringWriter();
			XMLWriter xmlWriter = new XMLWriter(stringWriter);
			xmlWriter.write(doc);
			xmlWriter.close();
			System.out.println( doc.asXML() );
			System.out.println( stringWriter.toString().trim());

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
 
Example 4
Project: appium-java-repl   File: Utils.java   Source Code and License Vote up 5 votes
/**
 * Pretty-Print XML.
 * 
 * @param xml The XML
 * @return A beautiful XML
 * @throws IOException
 * @throws DocumentException
 */
public static String prettyXML(final String xml) throws IOException, DocumentException {
  Document doc = DocumentHelper.parseText(xml);
  StringWriter sw = new StringWriter();
  OutputFormat format = OutputFormat.createPrettyPrint();
  format.setIndent(true);
  format.setIndentSize(3);
  XMLWriter xw = new XMLWriter(sw, format);
  xw.write(doc);
  return sw.toString();
}
 
Example 5
Project: btulz.transforms   File: XmlTransformerDom4j.java   Source Code and License Vote up 5 votes
@Override
protected void save(File outFolder, IDomain domain) throws Exception {
	String fileName = this.getSaveFilePath(outFolder.getPath(), domain);
	Document document = DocumentHelper.createDocument();
	// 领域模型
	Element domainElement = document.addElement("Domain");
	this.writeElement(domain, domainElement);
	// 模型
	for (IModel model : domain.getModels()) {
		Element modelElement = domainElement.addElement("Model");
		this.writeElement(model, modelElement);
		// 模型属性
		for (IProperty property : model.getProperties()) {
			Element propertyElement = modelElement.addElement("Property");
			this.writeElement(property, propertyElement);
		}
	}
	// 业务对象
	for (IBusinessObject businessObject : domain.getBusinessObjects()) {
		Element boElement = domainElement.addElement("BusinessObject");
		this.writeElement(businessObject, boElement);
		for (IBusinessObjectItem boItem : businessObject.getRelatedBOs()) {
			Element biElement = boElement.addElement("RelatedBO");
			this.writeElement(boItem, biElement);
		}
	}
	OutputFormat xmlFormat = OutputFormat.createCompactFormat();
	xmlFormat.setEncoding(XML_FILE_ENCODING);
	xmlFormat.setNewlines(true);
	xmlFormat.setIndent(true);
	xmlFormat.setIndent("  ");
	XMLWriter writer = new XMLWriter(new FileWriter(fileName), xmlFormat);
	writer.write(document);
	writer.close();
}
 
Example 6
Project: openmeetings   File: XmlExport.java   Source Code and License Vote up 5 votes
public static void toXml(Writer out, Document doc) throws Exception {
	OutputFormat outformat = OutputFormat.createPrettyPrint();
	outformat.setIndentSize(1);
	outformat.setIndent("\t");
	outformat.setEncoding(UTF_8.name());
	XMLWriter writer = new XMLWriter(out, outformat);
	writer.write(doc);
	writer.flush();
	out.flush();
	out.close();
}
 
Example 7
Project: openyu-commons   File: Dom4jHelper.java   Source Code and License Vote up 5 votes
/**
 * 建構格式化
 * 
 * @param encoding
 *            String
 * @return OutputFormat
 */
public static OutputFormat createOutputFormat(String encoding) {
	OutputFormat format = OutputFormat.createPrettyPrint();
	format.setEncoding(encoding);
	// format.setIndent(StringUtil.TAB);
	format.setIndent("  ");
	format.setLineSeparator(System.getProperty("line.separator"));
	format.setTrimText(true);
	return format;
}
 
Example 8
Project: community-edition-old   File: LockMethod.java   Source Code and License Vote up 5 votes
/**
 * @see org.alfresco.repo.webdav.WebDAVMethod#createXMLWriter()
 */
@Override
protected OutputFormat getXMLOutputFormat()
{
    OutputFormat outputFormat = new OutputFormat();
    outputFormat.setNewLineAfterDeclaration(false);
    outputFormat.setNewlines(false);
    outputFormat.setIndent(false);
    return outputFormat;
}
 
Example 9
Project: community-edition-old   File: DeleteMethod.java   Source Code and License Vote up 5 votes
@Override
protected OutputFormat getXMLOutputFormat()
{
    OutputFormat outputFormat = new OutputFormat();
    outputFormat.setNewLineAfterDeclaration(false);
    outputFormat.setNewlines(false);
    outputFormat.setIndent(false);
    return outputFormat;
}
 
Example 10
Project: community-edition-old   File: PropfindMethod.java   Source Code and License Vote up 5 votes
@Override
protected OutputFormat getXMLOutputFormat()
{
    OutputFormat outputFormat = new OutputFormat();
    outputFormat.setNewLineAfterDeclaration(false);
    outputFormat.setNewlines(false);
    outputFormat.setIndent(false);
    return outputFormat;
}
 
Example 11
Project: EasyML   File: WFGraph.java   Source Code and License Vote up 4 votes
/**
 * Transform the Graph into an workflow xml definition
 * @param jobname the job name of Oozie job, can't be null
 * @return workflow xml
 */
public String toWorkflow(String jobname) {
	Namespace xmlns = new Namespace("", "uri:oozie:workflow:0.4"); // root namespace uri
	QName qName = QName.get("workflow-app", xmlns); // your root element's name
	Element workflow = DocumentHelper.createElement(qName);
	Document xmldoc = DocumentHelper.createDocument(workflow);
	// Create workflow root
	workflow.addAttribute("xmlns", "uri:oozie:workflow:0.4");
	// <workflow-app name='xxx'></workflow-app>
	if (jobname == null || "".equals(jobname))
		workflow.addAttribute("name", "Not specified");
	else
		workflow.addAttribute("name", jobname);

	Queue<NodeDef> que = new LinkedList<NodeDef>();
	que.add(start);

	while (!que.isEmpty()) {
		NodeDef cur = que.remove();

		cur.append2XML(workflow);

		for (NodeDef toNode : cur.getOutNodes()) {
			toNode.delInNode(cur);
			if (toNode.getInDegree() == 0)
				que.add(toNode);
		}
	}

	// Set XML document format
	OutputFormat outputFormat = OutputFormat.createPrettyPrint();
	// Set XML encoding, use the specified encoding to save the XML document to the string, it can be specified GBK or ISO8859-1
	outputFormat.setEncoding("UTF-8");
	outputFormat.setSuppressDeclaration(true); // Whether generate xml header
	outputFormat.setIndent(true); // Whether set indentation
	outputFormat.setIndent("    "); // Implement indentation with four spaces
	outputFormat.setNewlines(true); // Set whether to wrap

	try {
		// stringWriter is used to save xml document
		StringWriter stringWriter = new StringWriter();
		// xmlWriter is used to write XML document to string(tool)
		XMLWriter xmlWriter = new XMLWriter(stringWriter, outputFormat);
		
		// Write the created XML document into the string
		xmlWriter.write(xmldoc);

		xmlWriter.close();

		System.out.println( stringWriter.toString().trim());
		// Print the string, that is, the XML document
		return stringWriter.toString().trim();

	} catch (Exception ex) {
		ex.printStackTrace();
	}
	return null;
}