本文整理汇总了Java中org.dom4j.io.OutputFormat.setExpandEmptyElements方法的典型用法代码示例。如果您正苦于以下问题:Java OutputFormat.setExpandEmptyElements方法的具体用法?Java OutputFormat.setExpandEmptyElements怎么用?Java OutputFormat.setExpandEmptyElements使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.dom4j.io.OutputFormat
的用法示例。
在下文中一共展示了OutputFormat.setExpandEmptyElements方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: printXML
import org.dom4j.io.OutputFormat; //导入方法依赖的package包/类
/**
* 打印XML
*
* @param document
*/
protected void printXML(Document document) {
if (log.isInfoEnabled()) {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setExpandEmptyElements(true);
format.setSuppressDeclaration(true);
StringWriter stringWriter = new StringWriter();
XMLWriter writer = new XMLWriter(stringWriter, format);
try {
writer.write(document);
log.info(stringWriter.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例2: testExpandEmptyElements
import org.dom4j.io.OutputFormat; //导入方法依赖的package包/类
@Test
public void testExpandEmptyElements() throws IOException {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element id = root.addElement("id");
id.addText("1");
root.addElement("empty");
OutputFormat xmlFormat = new OutputFormat();
// OutputFormat.createPrettyPrint();
xmlFormat.setSuppressDeclaration(true);
xmlFormat.setEncoding("UTF-8");
// If this is true, elements without any child nodes
// are output as <name></name> instead of <name/>.
xmlFormat.setExpandEmptyElements(true);
StringWriter out = new StringWriter();
XMLWriter xmlWriter = new XMLWriter(out, xmlFormat);
xmlWriter.write(document);
xmlWriter.close();
assertEquals("<root><id>1</id><empty></empty></root>", out.toString());
}
示例3: formatXml
import org.dom4j.io.OutputFormat; //导入方法依赖的package包/类
/**
* Returns the given xml document as nicely formated string.
*
* @param node
* The xml document.
* @return the formated xml as string.
*/
private static String formatXml(Node node) {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setIndentSize(4);
format.setTrimText(true);
format.setExpandEmptyElements(true);
StringWriter stringWriter = new StringWriter();
XMLWriter xmlWriter = new XMLWriter(stringWriter, format);
try {
xmlWriter.write(node);
xmlWriter.flush();
} catch (IOException e) {
// this should never happen
throw new RuntimeException(e);
}
return stringWriter.getBuffer().toString();
}