处理 JAXB 中的嵌套元素

发布于 2024-09-07 10:27:48 字数 390 浏览 5 评论 0原文

我想知道 JAXB 是否可以不为充当包装器的 XML 元素创建 Java 对象。例如,对于以下结构的 XML,

<root>
    <wrapper>
        <entity/>
    </wrapper>
</root>

我根本不希望创建 的对象。 类,

class Root {
    private Entity entity;
}

因此,对于像 这样的

应该直接将其解组到实体字段中。用JAXB可以实现吗?

I am wondering if it is possible to have JAXB not to create Java object for XML elements that serve as wrappers. For example, for XML of the following structure

<root>
    <wrapper>
        <entity/>
    </wrapper>
</root>

I do not want an object for <wrapper> to be created at all. So for a class like

class Root {
    private Entity entity;
}

the <entity> element should be unmarshalled directly into the entity field.

Is it possible to achieve with JAXB?

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

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

发布评论

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

评论(4

无悔心 2024-09-14 10:27:49

尽管需要额外的编码,但所需的解组是使用瞬态包装器对象通过以下方式完成的:

@XmlRootElement(name = "root")
public class Root {

    private Entity entity;

    static class Entity {

    }

    static class EntityWrapper {
        @XmlElement(name = "entity")
        private Entity entity;

        public Entity getEntity() {
            return entity;
        }
    }

    @XmlElement(name = "wrapper")
    private void setEntity(EntityWrapper entityWrapper) {
        entity = entityWrapper.getEntity();
    }

}

Although it requires extra coding, the desired unmarshalling is accomplished in the following way using a transient wrapper object:

@XmlRootElement(name = "root")
public class Root {

    private Entity entity;

    static class Entity {

    }

    static class EntityWrapper {
        @XmlElement(name = "entity")
        private Entity entity;

        public Entity getEntity() {
            return entity;
        }
    }

    @XmlElement(name = "wrapper")
    private void setEntity(EntityWrapper entityWrapper) {
        entity = entityWrapper.getEntity();
    }

}
宁愿没拥抱 2024-09-14 10:27:49

EclipseLink MOXy 提供带有扩展的 JAXB 2.2 实现。扩展功能之一是使用 XPath 浏览域模型中不需要的 XML 层。

如果您查看:

http://wiki.eclipse.org/EclipseLink/Examples/ MOXy/GettingStarted/MOXyExtensions

您会注意到客户的姓名存储在其中,但该名称是客户的字符串属性。这是通过以下方法完成的:

@XmlPath("personal-info/name/text()")
public String getName() {
    return name;
}

我希望这有帮助,

Doug

EclipseLink MOXy offers a JAXB 2.2 implementation with extensions. One of the extended capabilities is to use XPath to navigate through layers of the XML you don't want in you domain model.

If you look at:

http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/MOXyExtensions

you will notice that the Customer's name is stored within but that the name is a String attribute of Customer. This is accomplished using:

@XmlPath("personal-info/name/text()")
public String getName() {
    return name;
}

I hope this helps,

Doug

﹉夏雨初晴づ 2024-09-14 10:27:49

值得一提的是,如果内容是 列表而不是单个实例:

<root>
    <wrapper>
        <entity/>
        <entity/>
        ...
    </wrapper>
</root>

那么您可以使用 @XmlElementWrapper 注释:

@XmlRootElement(name = "root")
public class Root {

    @XmlElementWrapper(name = "wrapper")
    @XmlElement(name = "entity")
    private List<Entity> entity;

    static class Entity { }

}

Worth mentioning, if the content is a list of <entity/> instead of a single instance:

<root>
    <wrapper>
        <entity/>
        <entity/>
        ...
    </wrapper>
</root>

then you can use the @XmlElementWrapper annotation:

@XmlRootElement(name = "root")
public class Root {

    @XmlElementWrapper(name = "wrapper")
    @XmlElement(name = "entity")
    private List<Entity> entity;

    static class Entity { }

}
笑红尘 2024-09-14 10:27:49

JAXB 或其他映射系统的全部要点是将元素及其层次结构映射到类。在您的示例中,您似乎希望 JAXB 以某种方式知道它可以将实体编组到包装器/实体中,反之亦然,而无需实际创建用于包装器映射以及根与实体之间的连接的类。正如所介绍的,这大致相当于询问如何在没有驱动轴的情况下将汽车发动机连接到车轮。

因此,除非我没有抓住重点,否则答案是否定的——JAXB 或任何其他映射程序都无法做到这一点。您可以通过使用纯粹动态映射的东西来避免创建类(例如,请参阅 Groovy、GPath),但这可以避免创建所有类,而不仅仅是跳过层次结构中的一个中间级别。

The whole point of JAXB or other mapping systems is to map the elements and their hierarchy to classes. In your example, you seem to want JAXB to somehow know that it can marshal entity into wrapper/entity and vice-versa without actually creating the class used for the wrapper mapping and the connection between root and entity. Which, as presented, is roughly equivalent to asking how to connect a car engine to the wheels without a driveshaft.

So, unless I am missing the point, the answer is no - neither JAXB or any other mapping program can do this. You can avoid creating classes by using something that does mapping purely dynamically (see Groovy, GPath for an example), but that avoids creating all classes, not just skipping one intermediate level in a hierarchy.

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