本文整理匯總了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();
}