所以我们有一个XSD类型:
<xs:complexType name="Foo">
<xs:all>
<xs:element name="Bars">
<xs:complexType>
<xs:sequence>
<xs:element name="Bar" type="barType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
表示XML:
<Foo>
<!-- Elements snipped for brevity-->
<Bars>
<Bar>
<!-- Bar Element -->
</Bar>
</Bars>
</Foo>
xjc产生几乎正确的结果。唯一令人讨厌的事情是“Bars”被创建为一个内部类,它存储一个Bars列表。反正有没有Bar在Foo中成为List而仍然保留上面的XML?
答案 0 :(得分:6)
当您将Bars定义为复杂类型时,Bars将作为分隔的类生成。像这样我发现架构也更容易阅读。除非你将maxOccurs更改为大于1的值,否则Bars不会是Foo中的List - 你不能在xs:all上执行此操作,但你可以使用xs:sequence。
...
<xs:complexType name="Foo">
<xs:all>
<xs:element name="Bars" type="Bars" />
</xs:all>
</xs:complexType>
<xs:complexType name="Bars">
<xs:sequence>
<xs:element name="Bar" type="barType" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
...
运行xjc后: Foo.java:
...
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Foo", propOrder = {
})
public class Foo {
@XmlElement(name = "Bars", required = true)
protected Bars bars;
public Bars getBars() {
return bars;
}
public void setBars(Bars value) {
this.bars = value;
}
}
Bars.java:
...
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Bars", propOrder = {
"bar"
})
public class Bars {
@XmlElement(name = "Bar", required = true)
protected List<String> bar;
...
}
使用xs:seqence获取Bars列表(maxOccurs =“unbounded”): XSD:
...
<xs:complexType name="Foo">
<xs:sequence>
<xs:element name="Bars" type="Bars" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Bars">
<xs:sequence>
<xs:element name="Bar" type="barType" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
...
Foo.java:
...
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Foo", propOrder = {
"bars"
})
public class Foo {
@XmlElement(name = "Bars", required = true)
protected List<Bars> bars;
public List<Bars> getBars() {
if (bars == null) {
bars = new ArrayList<Bars>();
}
return this.bars;
}
}
答案 1 :(得分:4)
Bjarne Hansen的xjc插件(https://github.com/dmak/jaxb-xew-plugin)允许你使用“Bars”封闭元素,生成方便的@XmlElementWrapper(name =“Bars”)注释。
答案 2 :(得分:2)
另一种方法是放弃周围的&lt; bars&gt;元素,XML看起来不再那么好了,但它会使java代码更容易阅读。与xjc的简单绑定(参见http://jaxb.java.net/nonav/jaxb20-fcs/docs/vendorCustomizations.html)一起,它将生成相当漂亮且有用的java代码。
答案 3 :(得分:2)
也许以下示例有帮助。
XML Schema
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="test">
<xs:sequence>
<xs:element name="dataList" type="xs:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="str" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Java类:
public class Test {
protected List<String> dataList;
protected String str;
public List<String> getDataList() {
if (dataList == null) {
dataList = new ArrayList<String>();
}
return this.dataList;
}
public String getStr() {
return str;
}
public void setStr(String value) {
this.str = value;
}
}
编辑1:
您可以从java代码反向工程到xsd。 在JAVA_HOME / bin中有schemagen可执行文件。
提供您的java代码,它将使用您的java类为您创建XSD架构。
schemagen myObj1.java myObj2.java
答案 4 :(得分:1)
您可以执行以下操作:
package example;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Foo")
public class Foo {
private List<Bar> bar;
public List<Bar> getBar() {
return bar;
}
@XmlElementWrapper(name="Bars")
@XmlElement(name="Bar")
public void setBar(List<Bar> bar) {
this.bar = bar;
}
}
和
package example;
public class Bar {
}
然后,您可以使用以下代码处理XML;
package example;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Foo foo = (Foo) unmarshaller.unmarshal(new File("src/forum128/input.xml"));
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}