简体   繁体   English

Java XML DocumentBuilderFactory写空标签 <Tag /> 从空值

[英]Java XML DocumentBuilderFactory write empty tags <Tag /> from null values

Hey so I'm wondering how it is possible to write a empty tag for example <Category /> with the DocumentBuilderFactory (based off this resource , library javax.xml.parsers.*; ), as for the moment I'm having to apply an if condition if object.getCategory() != null then create the Category Tag otherwise ignore it. 嘿,所以我想知道如何用DocumentBuilderFactory (基于此资源 ,库javax.xml.parsers.*; )编写一个空标签,例如<Category /> ,就目前而言,如果条件为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);
}

And Schema 和架构

<xs:complexType name="data">
    <xs:all>
    <xs:element name="Category" type="xs:string" minOccurs="1" />
    <!-- other columns.. -->
    </xs:all>
</xs:complexType>

And I've noticed if I add a textnode that is null, the transformer.transform(source, result); 而且我注意到,如果我添加了一个null的textnode,则是converter.transform transformer.transform(source, result); will return with a heap of NullException errors. 将返回一堆NullException错误。 Is there a way to configure the transformer to know that the TextNode is intentionally left empty? 有没有一种方法可以配置转换器以知道TextNode被故意留空了? and in turn create <Category /> or <Category></Category> . 然后依次创建<Category /><Category></Category>

//add the Category
Element Category = doc.createElement("category");
Rows.appendChild(Category);
if(excel.getCategory() != null){
    Category.appendChild(doc.createTextNode(excel.getCategory()));
}

Here I'm adding the category element to Rows unconditionally, but only adding a text node child if getCategory() is non-null. 在这里,我无条件地将category元素添加到Rows ,但是仅当getCategory()为非null时才添加文本节点子元素。 If it is null, then this will create an empty category element, which will serialize to XML as <category /> . 如果为null,则将创建一个空的category元素,该元素将作为<category />序列化为XML。

If you want to be able to distinguish in the XML between a null value for excel.getCategory() and an empty string value, then the usual XML schema idiom for that is to make the element "nillable" 如果你希望能够在一间XML区分null的值excel.getCategory()和一个空字符串值,然后为通常的XML模式成语是使元素“的nillable”

<xs:complexType name="data">
    <xs:all>
    <xs:element name="Category" type="xs:string" nillable="true" />
    <!-- other columns.. -->
    </xs:all>
</xs:complexType>

and mark it with xsi:nil 并用xsi:nil标记

//add the Category
Element Category = doc.createElement("category");
Rows.appendChild(Category);
if(excel.getCategory() != null){
    Category.appendChild(doc.createTextNode(excel.getCategory()));
} else {
    Category.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,
                            "xsi:nil", "true");
}

This will produce 这将产生

<category />

when excel.getCategory().equals("") and excel.getCategory().equals("")

<category xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />

when excel.getCategory() == null excel.getCategory() == null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM