JAXB会创建无效的nillable元素

JAXB会创建无效的nillable元素

本文介绍了在针对xsd生成xml时,JAXB会创建无效的nillable元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的xsd中我有元素

In my xsd I have element

<xs:element name="MyDateElement" type="MyDateElementType" nillable="true" />

<xs:complexType name="MyDateElementType">
    <xs:simpleContent>
        <xs:extension base="xs:date">
            <xs:attribute name="state" type="xs:string" />
            <xs:attribute name="dateFrom" type="xs:date" />
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

我正在使用

<artifactId>cxf-codegen-plugin</artifactId>

从wsdl生成java类。

to generate java classes from wsdl.

所以这个插件生成这个java类:

So this plugin generate this java class:

public class ParrentClass
    implements Serializable
{

    @XmlElement(name = "MyDateElement", required = true, nillable = true)
    protected MyDateElementType MyDateElement;

// setter and getter

}

public class MyDateElement
    implements Serializable
{

    @XmlValue
    protected Date value;
    @XmlAttribute(name = "state")
    protected String state;
    @XmlAttribute(name = "dateFrom")
    protected Date dateFrom;

    // setter and getter

    }

我认为这仍然没问题。

所以现在当我创建具有空值的元素并且仅使用属性时

so now when I create element with null value and just with attributes

protected MyDateElement getDatumStav(String state) {
    MyDateElement element = new MyDateElement();
    element.setState(state);
    return element;
}

JAXB创建无效的xml:

JAXB create invalid xml:

<ns:MyDateElement stav="S"></ns:MyDateElement>

(缺少nillable = true)

(nillable=true is missing)

所以任何人都可以帮助我如何解决这个问题。

So can anyone helps me how should I solve this problem.

PS:
我知道在xsd中我允许 minOccurs = 0 然后插件生成java类,其中包含 JAXBElement< MyDateElement> 我可以手动设置 nillable 。但我想避免这个解决方案,因为这个元素是必需的

PS:I know that when in xsd I allow minOccurs=0 then plugin generate java class which contains JAXBElement<MyDateElement> where I can manually set nillable. But I want to avoid this solution because this element is required

PS:可能从XSD生成java类有bug,因为我发现了这个老bug:。但这应该修复,所以我仍然感到困惑

PS: Probably there is bug in generating java class from XSD because I found this old bug: https://java.net/jira/si/jira.issueviews:issue-html/JAXB-840/JAXB-840.html . But this should be fixed so I am still confused

推荐答案

你可以用JDOM2在java类中添加它:

You can add it in java class with JDOM2 :

例如:

Namespace ns0 = Namespace.getNamespace("ns0", "http://...");
Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
SAXBuilder jdomBuilder = new SAXBuilder();
InputStream stream = new ByteArrayInputStream(xmlFileString.getBytes("UTF-8"));
Document jdomDocument = jdomBuilder.build(stream);

Element root = jdomDocument.getRootElement();
Element agreement = root.getChild("Agreement", ns0);

Element co = agreement.getChild("CoOwner", ns0);
if (co.getText().equals(""))
{
    co.setAttribute("nil", "true", xsi);
}

// Return to string
return new XMLOutputter().outputString(jdomDocument);

输出:

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

这篇关于在针对xsd生成xml时,JAXB会创建无效的nillable元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:51