如何解决JDOM2- 不要自动关闭标签
我正在使用 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>
解决方法
尝试以下解决方案。
使用 Format
对象获取空元素的结束标记。
Format format = Format.getPrettyFormat();
format.setExpandEmptyElements(true);
XMLOutputter outter = new XMLOutputter(format);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。