使用 eclipselink MOXy 的 XmlPath 映射问题



我不明白为什么我在下面所做的 XmlPath 映射显示为空。我的语法有问题吗?我在其他地方使用了类似的语法,没有问题。

感谢您提供任何线索..John

<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"/>
      </param>
    </adjust-volume>
    <audio ref="r9" name="VoiceOver-26 - audio" duration="4639346/48000s" role="dialogue"/>
</clip>
@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;

我无法使用 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 {
}

DecibalValueAdapter

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;
    }
}

最新更新