嘿,所以我想知道如何<Category />使用DocumentBuilderFactory (基于此资源,库javax.xml.parsers.*;)编写一个空标签,因为目前我必须应用一个 if 条件if object.getCategory() != null然后创建Category Tag否则忽略它。
//add the Category
if(excel.getCategory() != null){
Element Category = doc.createElement("category");
Category.appendChild(doc.createTextNode(excel.getCategory()));
Rows.appendChild(Category);
}
和架构
<xs:complexType name="data">
<xs:all>
<xs:element name="Category" type="xs:string" minOccurs="1" />
<!-- other columns.. -->
</xs:all>
</xs:complexType>
而且我注意到如果我添加一个为空的文本节点,transformer.transform(source, result);它将返回一堆NullException错误。有没有办法配置转换器以知道 TextNode 是故意留空的?进而创建<Category />or <Category></Category>。