我无法重现您使用 EclipseLink 2.4.0 看到的问题。以下是我尝试过的。
夹子
您的映射似乎没问题。
package forum11937980;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name = "clip")
@XmlAccessorType(XmlAccessType.FIELD)
public class Clip extends StoryElement {
@XmlPath("adjust-volume/@amount")
@XmlJavaTypeAdapter(DecibelValueAdapter.class)
private Double adjustVolume;
@XmlPath("adjust-volume/param[@name='amount']/fadeIn/@duration")
@XmlJavaTypeAdapter(TimeValueAdapter.class)
private TimeValue fadeIn;
@XmlPath("adjust-volume/param[@name='amount']/fadeOut/@duration")
@XmlJavaTypeAdapter(TimeValueAdapter.class)
private TimeValue fadeOut;
}
jaxb.properties
您在与域模型相同的包中是否有一个 jaxb.properties 文件,其中包含以下条目以将 MOXy 指定为您的 JAXB 提供程序?
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
输入.xml
<clip lane="-1" offset="2591065664/720000s" name="Music" duration="22304160/240000s"
start="176794/48000s" enabled="0" format="r5">
<adjust-volume amount="1dB">
<param name="amount">
<fadeIn type="easeIn" duration="1220/262144s" />
<fadeOut duration="I/Added/This"/>
</param>
</adjust-volume>
<audio ref="r9" name="VoiceOver-26 - audio" duration="4639346/48000s"
role="dialogue" />
</clip>
演示
package forum11937980;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Clip.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11937980/input.xml");
Clip clip = (Clip) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(clip, System.out);
}
}
输出
下面是运行演示代码的输出。请注意,只有输入中的映射部分出现在输出中。
<?xml version="1.0" encoding="UTF-8"?>
<clip>
<adjust-volume amount="1.0dB">
<param name="amount">
<fadeIn duration="1220/262144s"/>
<fadeOut duration="I/Added/This"/>
</param>
</adjust-volume>
</clip>
支持文件
以下是运行此示例所需的其余文件:
故事元素
package forum11937980;
public class StoryElement {
}
十进制值适配器
package forum11937980;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class DecibelValueAdapter extends XmlAdapter<String, Double> {
@Override
public String marshal(Double v) throws Exception {
return String.valueOf(v) + "dB";
}
@Override
public Double unmarshal(String v) throws Exception {
return Double.valueOf(v.substring(0, v.length() - 2));
}
}
时间价值
package forum11937980;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class TimeValue {
@XmlValue
private String value;
}
时间值适配器
package forum11937980;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class TimeValueAdapter extends XmlAdapter<TimeValue, TimeValue> {
@Override
public TimeValue marshal(TimeValue v) throws Exception {
return v;
}
@Override
public TimeValue unmarshal(TimeValue v) throws Exception {
return v;
}
}