如何使用 xjc 和自定义绑定生成 XMLElementWrapper 注释

发布于 2024-08-25 23:09:42 字数 726 浏览 8 评论 0原文

我正在使用 JAXB 和 xjc 将 XML 架构编译为 Java 类。我不想手动编辑这个生成的类。我有这样的 xml 模式:

<xs:element name="root">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="items">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="item" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

xjc 生成一个类 Items ,它只包含 Item 对象的列表。是否有机会省略类 Items 并直接在 Root 类中拥有 Item 对象列表?

我知道这可以通过 @XMLElementWrapper 注释来完成,但我不知道如何告诉 xjc 创建这样的注释。

感谢您的任何建议!

此致, 马库斯

I'm using JAXB and xjc to compile my XML Schema into Java classes. I do not want to manually edit this generated classes. I have xml schema like that:

<xs:element name="root">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="items">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="item" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

The xjc generates a class Items that only contains a list of Item objects. Is there any chance to omit the class Items and have a list of Item objects directly in the Rootclass?

I know that this can be done with @XMLElementWrapper annotation, but I don't know how to tell xjc to create such.

Thanks for any suggestions!

Best regards,
Markus

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九八野马 2024-09-01 23:09:42

Bjarne Hansen 为 xjc 开发了一个插件来解决这个问题。不幸的是,原始实现的链接现已失效。不过,github 上有一个 Dmitry Katsubo 的项目,基于 Bjarne 的原始代码并进行了一些额外的改进。

https://github.com/dmak/jaxb-xew-plugin


(只是供参考:原始链接,现已失效:http://www.conspicio。 dk/blog/bjarne/jaxb-xmlelementwrapper-plugin)

Bjarne Hansen developed a plugin for xjc that was able to take care of this. Unfortunately, the link to the original implementation is now dead. However, there is a project by Dmitry Katsubo on github, based on Bjarne's original code with some additional improvements.

https://github.com/dmak/jaxb-xew-plugin


(Just for reference: the original link, now dead: http://www.conspicio.dk/blog/bjarne/jaxb-xmlelementwrapper-plugin)

娇纵 2024-09-01 23:09:42

首先,让我们分解您的架构,以便不会生成内部类:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
    <xs:element name="root" type="Root" />

    <xs:complexType name="Root">
        <xs:sequence>
            <xs:element name="items" type="Items" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Items">
        <xs:sequence>
            <xs:element name="item" type="xs:string" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

您仍然会获得额外的类,只是不是全部都在一个文件中。现在,您想要在构建中添加一个部分以使用 jaxb-xew-plugin。我使用 Maven,所以对我来说,这看起来像:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.8.2</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <args>
                    <arg>-no-header</arg>
                    <arg>-Xxew</arg>
                    <arg>-Xxew:instantiate lazy</arg>
                    <arg>-Xxew:delete</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>com.github.jaxb-xew-plugin</groupId>
                        <artifactId>jaxb-xew-plugin</artifactId>
                        <version>1.0</version>
                    </plugin>
                </plugins>
            </configuration>
        </execution>
    </executions>
</plugin>

如果您开始使用名称空间,以便生成的类具有包名称,请忽略 -Xxew:delete 标志,因为我最近修复了一个错误,其中它正在删除不应该删除的对象。或者,您可以从 github 获取代码并将其用作 1.1-SNAPSHOT。

当我这样做时,我会生成我认为您正在寻找的代码:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Root", propOrder = {
    "items"
})
public class Root {

    @XmlElementWrapper(name = "items", required = true)
    @XmlElement(name = "item")
    protected List<String> items;

    public List<String> getItems() {
        if (items == null) {
            items = new ArrayList<String>();
        }
        return items;
    }

    public void setItems(List<String> items) {
        this.items = items;
    }

}

First lets break up your schema so that there are no inner classes generated:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
    <xs:element name="root" type="Root" />

    <xs:complexType name="Root">
        <xs:sequence>
            <xs:element name="items" type="Items" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Items">
        <xs:sequence>
            <xs:element name="item" type="xs:string" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

You'll still get extra classes, just not all in one file. Now you want to add a section to your build to use the jaxb-xew-plugin. I use Maven, so for me this looks like:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.8.2</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <args>
                    <arg>-no-header</arg>
                    <arg>-Xxew</arg>
                    <arg>-Xxew:instantiate lazy</arg>
                    <arg>-Xxew:delete</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>com.github.jaxb-xew-plugin</groupId>
                        <artifactId>jaxb-xew-plugin</artifactId>
                        <version>1.0</version>
                    </plugin>
                </plugins>
            </configuration>
        </execution>
    </executions>
</plugin>

If you start using namespaces so that your generated classes have package names, leave off the -Xxew:delete flag, as there's a bug that I recently fixed where it was deleting objects it shouldn't. Alternatively, you could grab the code from github and use it as 1.1-SNAPSHOT.

When I do that I get the code generated that I think you're looking for:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Root", propOrder = {
    "items"
})
public class Root {

    @XmlElementWrapper(name = "items", required = true)
    @XmlElement(name = "item")
    protected List<String> items;

    public List<String> getItems() {
        if (items == null) {
            items = new ArrayList<String>();
        }
        return items;
    }

    public void setItems(List<String> items) {
        this.items = items;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文