解组后保持亲子关系 [英] Keep child-parent relationship after unmarshalling

查看:69
本文介绍了解组后保持亲子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我正在尝试使用JAXB解组的示例:

Here is an example of what I'm trying to unmarshall with JAXB:

<?xml version="1.0" encoding="UTF-8"?>
<menus>
    <menu>
        <name>main</name>
        <subMenu>
            <name>mainMenu</name>
            <!-- Transfer / Versement -->
            <subMenu>
                <name>transfer</name>
                <label>MENU_TRANSFER</label>
                <icon>call-received</icon>

                <menuItem>
                    <name>record</name>
                    <label>MENU_RECORD</label>
                    <url>/pages/record/search/recordListSearchResult.jsf</url>
                </menuItem>

                <menuItem>
                    <name>transferInput</name>
                    <label>MENU_TRANSFER_OPEN</label>
                    <url>/pages/transfer/open/transferListOpen.jsf</url>
                </menuItem>
        </subMenu>
    </menu>
</menus>

一旦解组,使用java生成的代码,我希望能够检索menuItem的子菜单父级(不必使用循环等)基本上,我想在MenuItem上生成一个getter getParent ,返回他的父亲。有没有一种简单的方法呢?

Once unmarshalled, with java generated code, I want to be able to retrieve the submenu parent of a menuItem (without having to use loop etc.) Basically, I want to generate a getter getParent on MenuItem returning his parent. Is there an easy way to do this?

推荐答案

你可以使用解组事件回调,以便在解组后在映射类的实例中接收父对象。为此,只需添加一个带有以下签名的方法:

You can use Unmarshal Event Callbacks to receive parent object in the instance of your mapped class after unmarshalling. To do this simply add a method with the following signature:

void afterUnmarshal(Unmarshaller unmarshaller, Object parent);

因此,您将在解组后获得子子菜单中的父子菜单。

Thus you'll get the parent submenu in the child submenu after unmarshalling.

如果映射的类是模式派生的(例如,您使用XJC编译一些XML模式),则可以使用代码注入器插件将代码添加到生成的类中。

If your mapped classes are schema-derived (for example you compile some XML schema with XJC), you can use the code injector plugin to add code to generated classes.

我还认为应该有XJC插件来处理父对象(谷歌XJC或JAXB父插件)。

I also think there should be XJC plugins for handling parent objects (google XJC or JAXB parent plugin).

这篇关于解组后保持亲子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆