Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用MOXy绑定文件处理java.util.Date_Date_Jaxb_Eclipselink_Moxy - Fatal编程技术网

如何使用MOXy绑定文件处理java.util.Date

如何使用MOXy绑定文件处理java.util.Date,date,jaxb,eclipselink,moxy,Date,Jaxb,Eclipselink,Moxy,一般来说,我不熟悉MOXy和JaxB,并且在java.util.Date转换方面遇到了问题 我使用映射文件将XML文件(我无法控制)解组为对象(我既不能手动注释现有类,也不能更改它们) 我的XML映射文件如下所示: <?xml version="1.0"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" version="2.1">

一般来说,我不熟悉MOXy和JaxB,并且在java.util.Date转换方面遇到了问题

我使用映射文件将XML文件(我无法控制)解组为对象(我既不能手动注释现有类,也不能更改它们)

我的XML映射文件如下所示:

<?xml version="1.0"?>
<xml-bindings 
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        version="2.1">
    <java-types>
        <java-type name="Observation">
            <xml-type prop-order="date theoricalTime ci ch cr type" />
            <java-attributes>
                <xml-element java-attribute="date" xml-path="Date/text()" />
                <xml-element java-attribute="theoricalTime" xml-path="TheoricalTime/text()" />
                <xml-element java-attribute="ci" xml-path="CIPR/text()" />
                <xml-element java-attribute="ch" xml-path="CHPR/text()" />
                <xml-element java-attribute="cr" xml-path="CRPR/text()" />
                <xml-element java-attribute="type" xml-path="Type/text()" />
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>
我的问题是:是否可以使用MOXy的外部元数据在映射文件中指定类型转换?如何使用上面指定的格式处理datetime和time,并将它们映射到日期字段

(我私下希望布莱斯·道格汉正在读这篇文章。)


提前感谢您的帮助

以下内容演示了如何将
XmlAdapter
与MOXy的外部映射文档一起使用,以获得所需的结果:

日期适配器

由于您的日期/时间数据采用以下格式
dd/MM/yyyy HH:MM:ss
,因此需要使用
XmlAdapter
,如下所示:

package forum8745305;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateAdapter extends XmlAdapter<String, Date> {

    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

    @Override
    public String marshal(Date v) throws Exception {
        return dateFormat.format(v);
    }

    @Override
    public Date unmarshal(String v) throws Exception {
        return dateFormat.parse(v);
    }

}
观察

根据您的问题,下面是您的域类的外观:

package forum8745305;

import java.util.Date;

public class Observation {

    private Date date;
    private Date theoricalTime;
    private String numeroTrain;
    private String ci;
    private String ch;
    private String cr;
    private String type;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getTheoricalTime() {
        return theoricalTime;
    }

    public void setTheoricalTime(Date theoricalTime) {
        this.theoricalTime = theoricalTime;
    }

    public String getNumeroTrain() {
        return numeroTrain;
    }

    public void setNumeroTrain(String numeroTrain) {
        this.numeroTrain = numeroTrain;
    }

    public String getCi() {
        return ci;
    }

    public void setCi(String ci) {
        this.ci = ci;
    }

    public String getCh() {
        return ch;
    }

    public void setCh(String ch) {
        this.ch = ch;
    }

    public String getCr() {
        return cr;
    }

    public void setCr(String cr) {
        this.cr = cr;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}
演示

以下代码可用于运行该示例:

package forum8745305;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8745305/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Observation.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum8745305/input.xml");
        Observation observation = (Observation) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(observation, System.out);
    }

}

它适用于所有datetime字段(包含“01/02/2012 18:02:34”的字段),但我仍然不知道如何映射只包含时间或日期的字段(“14:01:34”和“dd/MM/yyyy”),哪些字段应该映射到日期字段?我是否应该为每种格式创建3个SimpleDataFormat(“dd/MM/yyyy HH:MM:ss”、“dd/MM/yyyy”和“HH:MM:ss”)并测试字符串的内容以了解应该使用哪种SimpleDataFormat?或者有没有办法为每个属性指定一个特定的方法?@user1121108-我已经更新了我的答案,如果您在属性级别指定了
XmlAdapter
,XML元数据会是什么样子。根据您的评论,对于每种所需格式,您都需要单独的
XmlAdapters
。@user1121108:如果您有不同格式的字符串,您应该有一个
XmlAdapter
,但尝试几种模式进行解析(从最长的开始)。不幸的是,您无法将
日期
正确转换回
字符串
:在
日期
中没有未设置字段的概念。考虑映射到<代码>部分< /代码>。感谢这些答案,这非常有帮助,有没有地方可以找到关于MOXY外部元数据映射的不同可能性的文档?@ USER 1121108——MOXY用户指南()是您最好的资源。每个部分描述如何使用注释和XML进行相关映射。
package forum8745305;

import java.util.Date;

public class Observation {

    private Date date;
    private Date theoricalTime;
    private String numeroTrain;
    private String ci;
    private String ch;
    private String cr;
    private String type;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getTheoricalTime() {
        return theoricalTime;
    }

    public void setTheoricalTime(Date theoricalTime) {
        this.theoricalTime = theoricalTime;
    }

    public String getNumeroTrain() {
        return numeroTrain;
    }

    public void setNumeroTrain(String numeroTrain) {
        this.numeroTrain = numeroTrain;
    }

    public String getCi() {
        return ci;
    }

    public void setCi(String ci) {
        this.ci = ci;
    }

    public String getCh() {
        return ch;
    }

    public void setCh(String ch) {
        this.ch = ch;
    }

    public String getCr() {
        return cr;
    }

    public void setCr(String cr) {
        this.cr = cr;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}
package forum8745305;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8745305/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Observation.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum8745305/input.xml");
        Observation observation = (Observation) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(observation, System.out);
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<observation>
    <Date>05/01/2012 16:36:24</Date>
    <TheoricalTime>01/02/2012 12:34:45</TheoricalTime>
</observation>
<?xml version="1.0"?>
<xml-bindings 
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        version="2.1"
        package-name="forum8745305">
    <java-types>
        <java-type name="Observation">
            <xml-type prop-order="date theoricalTime ci ch cr type" />
            <xml-root-element/>
            <java-attributes>
                <xml-element java-attribute="date" xml-path="Date/text()">
                    <xml-java-type-adapter value="forum8745305.DateAdapter"/>
                </xml-element>
                <xml-element java-attribute="theoricalTime" xml-path="TheoricalTime/text()">
                    <xml-java-type-adapter value="forum8745305.DateAdapter"/>
                </xml-element>
                <xml-element java-attribute="numeroTrain" xml-path="NumeroTrain/text()" />
                <xml-element java-attribute="ci" xml-path="CIPR/text()" />
                <xml-element java-attribute="ch" xml-path="CHPR/text()" />
                <xml-element java-attribute="cr" xml-path="CRPR/text()" />
                <xml-element java-attribute="type" xml-path="Type/text()" />
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>