gpt4 book ai didi

java - Smooks - 如何将选择元素从 xml 映射到 java

转载 作者:太空宇宙 更新时间:2023-11-04 07:38:07 25 4
gpt4 key购买 nike

在smooks(版本1.5.1)中是否可以仅将两个选择元素中的一个生成元素映射到java字段?

示例:

xsd 文件:

<complexType name="timeType">
<sequence>
<choice minOccurs="1" maxOccurs="1">
<element name="time" minOccurs="0" maxOccurs="1">
<complexType>
<attribute name="v" type="dateTime"/>
</complexType>
</element>
<element name="time2" minOccurs="0" maxOccurs="1">
<complexType>
<attribute name="v" type="dateTime"/>
</complexType>
</element>
</choice>
</complexType>

a) xml 文件1:

<parent>
<time v="2001-12-31T12:00:00"/>
</parent>

b) xml 文件2:

<parent> 
<time2 v="2002-12-31T12:00:00"/>
</parent>

smooks 映射

<jb:bean beanId="timeRef" class="someClass" createOnElement="parent">
<!-- only if is generated (present) time element map this time element-->
<jb:value property="fromHour" data="time/@v" decoder="DateTime" />

<!-- only if is generated (present) time2 element map time2 element -->
<jb:value property="fromHour" data="time2/@v" decoder="DateTime" />
</jb:bean>

我如何告诉 smooks,我只想保存一个生成的元素?谢谢

最佳答案

我的解决方案:

<jb:bean beanId="parentBeanId" class="parentMapClass" createOnElement="parentElement">
...
<jb:wiring property="property" beanIdRef="timeRef" />
<jb:wiring property="property" beanIdRef="timeIntervalRef" />
...
</jb:bean>

<jb:bean beanId="timeRef" class="someClass" createOnElement="parentElement/time">
<jb:value property="fromHour" data="time/@v" decoder="DateTime" />
<jb:value property="toHour" data="time/@v" decoder="DateTime" />
</jb:bean>

<jb:bean beanId="timeIntervalRef" class="someClass" createOnElement="parentElement/timeInterval">
<jb:value property="fromHour" data="timeInterval/@v" decoder="TimeIntervalFrom" />
<jb:value property="toHour" data="timeInterval/@v" decoder="TimeIntervalTo" />
</jb:bean>

关于java - Smooks - 如何将选择元素从 xml 映射到 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16491117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com