使用eclipselink MOXy进行XmlPath映射问题

时间:2012-08-13 15:59:05

标签: java jaxb eclipselink moxy

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

感谢任何线索......约翰

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

1 个答案:

答案 0 :(得分:0)

我无法重现您使用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

<强> input.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>

支持文件

以下是运行此示例所需的其余文件:

StoryElement

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

}

TIMEVALUE

package forum11937980;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class TimeValue {

    @XmlValue
    private String value;

}

TimeValueAdapter

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

}