gpt4 book ai didi

java - 使用 eclipselink MOXy 的 XmlPath 映射问题

转载 作者:行者123 更新时间:2023-12-01 05:11:44 26 4
gpt4 key购买 nike

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

感谢您提供任何线索..约翰

<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

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>
<小时/>

支持文件

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

故事元素

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;

}

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

}

关于java - 使用 eclipselink MOXy 的 XmlPath 映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11937980/

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