我正在使用 JDOM 生成一个 XML 文件。由于我没有在元素中放入任何内容,它会自动关闭。这不是我想要的行为,因为用户应该填充文件。是否可以使元素不自动关闭?
public void generateMigrationFile(String fileName) throws IOException {
Document migrationDocument=new Document();
Namespace namespace = Namespace.getNamespace("http://www.liquibase.org/xml/ns/dbchangelog");
Element databaseChangelogElement = new Element("databaseChangeLog", namespace);
Namespace XSI = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
databaseChangelogElement.addNamespaceDeclaration(XSI);
databaseChangelogElement.setAttribute("schemaLocation", "http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd", XSI);
Element changeSetElement=new Element("changeSet",namespace);
changeSetElement.setAttribute("author","");
changeSetElement.setAttribute("id",UUID.randomUUID().toString());
databaseChangelogElement.addContent(changeSetElement);
migrationDocument.setRootElement(databaseChangelogElement);
XMLOutputter outter=new XMLOutputter();
outter.setFormat(Format.getPrettyFormat());
outter.output(migrationDocument, new FileWriter(new File("migration_files\\"+fileName+".xml")));
//outter.output(migrationDocument, new FileWriter(new File("C:\\Users\\fabio\\Desktop\\Migrations\\migration_files\\"+fileName+".xml")));
System.out.println("Migration file generated successfully");
}
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet author="" id="5d4dbc70-39f4-4079-9fc0-7cac291f1c04" />
</databaseChangeLog>
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet author="" id="5d4dbc70-39f4-4079-9fc0-7cac291f1c04">
</changeSet>
</databaseChangeLog>
答案 0 :(得分:1)
尝试以下解决方案。
使用 Format
对象获取空元素的结束标记。
Format format = Format.getPrettyFormat();
format.setExpandEmptyElements(true);
XMLOutputter outter = new XMLOutputter(format);