假设我们在XSD中定义了一个集合类型
<xs:complexType name="Foos">
<xs:sequence>
<xs:element name="foo" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name="bar" type="xs:string"/>
<xs:element name="baz" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
使用XJC从中生成Java代码时,类型大致转换为
public class Foos {
public List<Foos.Foo> getFoos();
public static class Foo {
public String getBar();
public String getBaz();
}
}
Run Code Online (Sandbox Code Playgroud)
由于集合类型是某些其他类型的一部分,例如文档的根,所生成代码的客户端代码看起来有点像这样
for(Foo foo : document.getFoos().getFoos())
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
如果不手动编写包装器,有没有办法让客户端代码不那么难看?
它看起来应该是这样的
for(Foo foo : document.getFoos())
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
谢谢
人们编写了XJC插件来生成@XmlElementWrapper
注释,而不是拥有额外的包装类.
另外,您可以使用@XmlElementWrapper
自己创建类,并通过执行以下操作让生成的类引用它:
文献
您可以手工创建自己的Document
类以获得所需的行为.您可以通过利用@XmlElementWrapper
注释来获取分组元素,从而获得您正在寻找的行为.
package forum18247182;
import java.util.*;
import javax.xml.bind.annotation.*;
public class Document {
private List<Foos.Foo> foos = new ArrayList<Foos.Foo>();
@XmlElementWrapper
@XmlElement(name="foo")
public List<Foos.Foo> getFoos() {
return foos;
}
}
Run Code Online (Sandbox Code Playgroud)
XML Schema(schema.xsd)
这是一个基于我将使用的片段的扩展XML模式.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/schema"
xmlns="http://www.example.org/schema"
elementFormDefault="qualified">
<xs:element name="document" type="Document"/>
<xs:complexType name="Document">
<xs:sequence>
<xs:element name="foos" type="Foos"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Foos">
<xs:sequence>
<xs:element name="foo" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name="bar" type="xs:string" />
<xs:element name="baz" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
从XML模式生成Java模型时利用现有类(binding.xml)
我们将使用外部绑定文件来指示在类生成期间我们希望将现有类用于所调用的复杂类型Document
.
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="schema.xsd">
<jxb:bindings node="//xs:complexType[@name='Document']">
<jxb:class ref="forum18247182.Document"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)
XJC电话
我们将使用该-b
选项指定我们的绑定文件.我们还将使用该-p
选项强制生成的类的包名称与我们Document
类的包名称相匹配.我们也可以使我们Document
类的包名称与从XML模式生成类所产生的包名称相匹配.
xjc -b binding.xml -p forum18247182 schema.xsd
Run Code Online (Sandbox Code Playgroud)
演示代码
package forum18247182;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import forum18247182.Foos.Foo;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("forum18247182");
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource("src/forum18247182/input.xml");
Document document = unmarshaller.unmarshal(xml, Document.class).getValue();
for(Foo foo : document.getFoos())
{
System.out.println(foo);
}
}
}
Run Code Online (Sandbox Code Playgroud)
产量
以下是运行演示代码的输出:
forum18247182.Foos$Foo@51f3336e
forum18247182.Foos$Foo@35b5a4ca
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1848 次 |
最近记录: |