问题描述
我的应用程序正在调用webservice,我已经使用maven-jaxb2-plugin从WSDL / XSD生成了Java类。 webservice调用工作正常一段时间,但最近我遇到了将对象编组为XML的问题:
My application is calling a webservice and I have generated the Java classes from the WSDL/XSDs with the maven-jaxb2-plugin. The webservice calls worked fine for a while but recently I had a problem on marshalling an object into XML:
[org.xml.sax.SAXParseException: cvc-complex-type.2.4.d: Invalid content was found starting with element 'ns1:TheFooAndBarThing'.
No child element '{"http://www.myschemanamespace.xyz/v1":BarId}' is expected at this point.]
XSD部分如下所示:
The XSD part looks like this:
<xs:complexType name="TheFooAndBarThing">
<xs:sequence>
<xs:element name="FooId" minOccurs="1" maxOccurs="1" type="nx:FooIdType"/>
<xs:element name="BarId" minOccurs="1" maxOccurs="100" type="nx:BarIdType"/>
</xs:sequence>
</xs:complexType>
生成的类 TheFooAndBarThing
如下所示(删除了Javadoc):
The generated class TheFooAndBarThing
looks like this (Javadoc removed):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TheFooAndBarThing", propOrder = {
"fooId",
"barId"
})
public class TheFooAndBarThing {
@XmlElement(name = "FooId", required = true)
protected String fooId;
@XmlElement(name = "BarId", required = true)
protected List<String> barId;
public String getFooId() {
return fooId;
}
public void setFooId(String value) {
this.fooId = value;
}
public List<String> getBarId() {
if (barId == null) {
barId = new ArrayList<String>();
}
return this.barId;
}
}
花了我一些时间和咖啡找出真正的问题。我的错误是我在列表中放置了超过100个 BarId
元素。
It cost me some time and coffee to find out the real problem. My mistake was that I put more than 100 BarId
elements in my list.
所以这是我的问题:
如何从XSD获取maxOccurs / minOccurs值到我的Java代码中,以便在构建元素列表时将其用作最大/最小值?
So here's my question:
How can I get the maxOccurs/minOccurs value from the XSD into my Java code so that I can use it as a max/min value while building my list of elements?
推荐答案
简答:没有简单的方法。
Short answer: there is no easy way.
模式派生类再也没有引用原始模式了。即使您使用XSOM或其他东西来解析原始模式,您也无法找到相应的XML Schema结构来检查。
Schema-derived classes have no reference to the original schema anymore. Even if you use something as XSOM or whatever to parse the original schema, you will not be able to find corresponding XML Schema constructs to check.
解决这个问题的最佳方法问题是写一个自定义的(我写了)。
The best way to address the issue would be writing a custom XJC plugin (I wrote quite a few of them).
当XJC编译时schmema它首先创建一个模型,然后是一个所谓的轮廓(预渲染代码),然后渲染代码。该模型仍然包含有关原始XML Schema结构的信息,因此您可以在那里找到所有相关的min / maxOccurs信息。
When XJC compiles the schmema it first creates a model, then a so-called outline (pre-rendered code) and then renders the code. The model still has information about the original XML Schema constructs, so you can find all the relevant min/maxOccurs information there.
问题在于您并不总是在模式构造和模式派生类的属性之间进行1:1映射。有时几个元素映射到一个属性。有大量例外和特殊情况。不过,你可以让它适用于直截了当的案例。无论如何,这项任务并不容易。
The problem is just that you don't always have 1:1 mapping between schema constructs and properties of schema-derived classes. Sometimes several elements are mapped onto one property. There's a huge number of exceptions and special cases. You can get it working for the straightforward cases, though. Anyway, the task is not easy.
这篇关于如何使用JAXB从Java中获取XSD的minOccurs / maxOccurs值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!