XmlPath mapping problems using eclipselink MOXy

别来无恙 提交于 2020-01-05 07:22:16

问题


I don't see why the XmlPath mappings I have made below are coming out as null. Is there something wrong with my syntax? I used similar syntax elsewhere without problem.

Thanks for any clues.. 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;

回答1:


I have not been able to reproduce the issue that you are seeing using EclipseLink 2.4.0. Below is what I have tried.

Clip

Your mappings appear to be ok.

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

Do you have a jaxb.properties file in the same package as your domain model with the following entry to specify MOXy as your JAXB provider?

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>

Demo

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

}

Output

Below is the output from running the demo code. Note that only the mapped portions from the input appear in the output.

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

SUPPORTING FILES

Below are the rest of the files needed to make this example run:

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

}


来源:https://stackoverflow.com/questions/11937980/xmlpath-mapping-problems-using-eclipselink-moxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!