我现在用的是Dom4j,将数据读入生成一个XML文件中<profiles>
<Allow value="中文"></Allow >
<Allow value="英文"></Allow >
<Allow value="法文"></Allow >
</profiles>现在格式要改成下面这个样子的XML<profiles>
<Allow value="中文"/>
<Allow value="英文"/>
<Allow value="法文"/>
</profiles>
怎么弄呢?各位大侠..
<Allow value="中文"></Allow >
<Allow value="英文"></Allow >
<Allow value="法文"></Allow >
</profiles>现在格式要改成下面这个样子的XML<profiles>
<Allow value="中文"/>
<Allow value="英文"/>
<Allow value="法文"/>
</profiles>
怎么弄呢?各位大侠..
XMLWriter.endElement() has "hadContent" hardcoded to true, so when using the ContentHandler interface methods it always writes a long closing tag for empty elements (looks like going through writeElement() does support short close).
Element profiles = document.addElement("profiles");
profiles.addElement("Allow").addAttribute("value","AABC");
XMLWriter output = new XMLWriter(new FileWriter( new File("c:/catalog.xml") ));
output.write( document );
output.close();
我这样就生成你要的格式了呀
我搞定了..谢谢大家的参与,这是代码.
把 flag = false 就是我要的格式。
flag = true 就会生成第一种格式public void saveDocument(Document doc, File file, String encode,boolean flag)
throws IOException
{
XMLWriter xmlWriter = null;
OutputStream os = null;
try
{
os = new FileOutputStream(file);
//设置XML编码,对XML头的编码和文件本身编码同时有效 OutputFormat format = OutputFormat.createPrettyPrint();
format.setIndent(" "); // 以空格方式实现缩进
format.setNewlines(true); // 设置是否换行
format.setIndent(true);
format.setEncoding(encode.toUpperCase());
format.setExpandEmptyElements(flag);
xmlWriter = new XMLWriter(os, format);
xmlWriter.write(doc);
xmlWriter.flush();
xmlWriter.close();
xmlWriter = null;
}
catch (Exception e)
{
// throw new XmlSaveException(e);
}
finally
{
colseXMLWriter(xmlWriter);
}
}