如何将XmlGregorianCalendar与fromJson和toJson方法一起使用? [英] How to use of XmlGregorianCalendar with fromJson and toJson methods of gson?

查看:384
本文介绍了如何将XmlGregorianCalendar与fromJson和toJson方法一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目的主题是提供XML格式的数据并使用google-gson获取Json格式,并且我从JAXB生成了XML架构中的Java POJO,其中我有一个XMLGregorianCalendar数据类型变量。

我给出以下XML输入并从gson.toJson()方法获得json格式;

 <?xml version =1.0encoding =UTF-8?> 
< EmpRequest xmlns =http://java.com/Employee>
< EmplIn>
< EmpID> 12< / EmpID>
< Empname> sara< / Empname>
<指定> SA< /指定>
< DOJ> 2002-05-30T09:30:10 + 06:00< / DOJ>
< / EmplIn>
< / EmpRequest>

但是在输出中,我得到了以下内容。

  {emplIn:{empID:12,empname:sara,指定:SA,doj:{}} } 

我浏览了google并得到了在xml架构中添加和使用字符串更改XmlGregorianCalendar数据类型的建议。但我不希望从两种方式实现它。



我的意思是如何通过fromJson和json的gson方法获得正确的输出与XmlGregorianCalendar数据类型?



非常感谢,
Harish Raj。

解决方案

注意:我是 EclipseLink JAXB(MOXy) 负责人,并且是 JAXB 2(JSR-222)的成员, 专家组。

您可以使用MOXy来处理此用例的XML和JSON绑定方面。正如我在我的评论中提到的,MOXy支持 XMLGregorianCalendar 类型。元数据如下所示:

EmpRequest

  package forum7725188; 

import javax.xml.bind.annotation。*;

@XmlRootElement(name =EmpRequest)
@XmlAccessorType(XmlAccessType.FIELD)
public class EmpRequest {

@XmlElement(name = EmplIn)
private emplIn emplIn;




EmplIn

  package forum7725188; 

import javax.xml.bind.annotation。*;
import javax.xml.datatype.XMLGregorianCalendar;

@XmlAccessorType(XmlAccessType.FIELD)
public class emplIn {

@XmlElement(name =EmpID)
private long empId;

@XmlElement(name =Empname)
私人字符串名称;

@XmlElement(name =Designation)
私有字符串指定;

@XmlElement(name =DOJ)
private XMLGregorianCalendar doj;

$ b

包信息

  @XmlSchema(namespace =http://java.com/Employee,elementFormDefault = XmlNsForm.QUALIFIED)
@ XmlAccessorType(XmlAccessType.FIELD)
包论坛7725188;

import javax.xml.bind.annotation。*;

演示

您可以通过将 eclipselink.media-type 属性设置为是,将 Marshaller 的MOXy实现配置为输出JSON application / json

  package forum7725188; 

import java.io.File;
import javax.xml.bind。*;
import javax.xml.namespace.QName;

public class Demo {

public static void main(String [] args)throws Exception {
JAXBContext jc = JAXBContext.newInstance(EmpRequest.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
文件xml =新文件(src / forum7725188 / input.xml);
EmpRequest empRequest =(EmpRequest)unmarshaller.unmarshal(xml);

JAXBElement< EmpRequest> jaxbElement = new JAXBElement< EmpRequest>(new QName(),EmpRequest.class,empRequest);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.setProperty(eclipselink.media-type,application / json);
marshaller.marshal(jaxbElement,System.out);
}

}

input.xml

 <?xml version =1.0encoding =UTF-8?> 
< EmpRequest xmlns =http://java.com/Employee>
< EmplIn>
< EmpID> 12< / EmpID>
< Empname> sara< / Empname>
<指定> SA< /指定>
< DOJ> 2002-05-30T09:30:10 + 06:00< / DOJ>
< / EmplIn>
< / EmpRequest>

输出

  {EmplIn:
{EmpID:12,
Empname:sara,
Designation: SA,
DOJ:2002-05-30T09:30:10 + 06:00}}

更多信息




The theme of my project is to give XML format of data and get Json format using google-gson and I have JAXB generated java POJOs from XML schema in which I have a variable of XMLGregorianCalendar datatype.

I give the following input of XML and get the json format from the gson.toJson() method;

<?xml version="1.0" encoding="UTF-8"?>
<EmpRequest xmlns="http://java.com/Employee">
<EmplIn>
<EmpID>12</EmpID>
<Empname>sara</Empname>
<Designation>SA</Designation>
<DOJ>2002-05-30T09:30:10+06:00</DOJ>
</EmplIn>
</EmpRequest>

But in the output, I got the following.

{"emplIn":{"empID":"12","empname":"sara","designation":"SA","doj":{}}}

I surfed google and got the suggestion of adding in the xml schema and changing the XmlGregorianCalendar datatype with string. But I dont want to achieve it from both the ways.

I mean how to get the proper output with the XmlGregorianCalendar datatype through fromJson and toJson methods of gson?

Thank you so much, Harish Raj.

解决方案

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

You could use MOXy to handle both the XML and JSON binding aspects of this use case. As I mentioned in my comment MOXy supports the XMLGregorianCalendar type. The Metadata would look like:

EmpRequest

package forum7725188;

import javax.xml.bind.annotation.*;

@XmlRootElement(name="EmpRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class EmpRequest {

    @XmlElement(name="EmplIn")
    private EmplIn emplIn;

}

EmplIn

package forum7725188;

import javax.xml.bind.annotation.*;
import javax.xml.datatype.XMLGregorianCalendar;

@XmlAccessorType(XmlAccessType.FIELD)
public class EmplIn {

    @XmlElement(name="EmpID")
    private long empId;

    @XmlElement(name="Empname")
    private String name;

    @XmlElement(name="Designation")
    private String designation;

    @XmlElement(name="DOJ")
    private XMLGregorianCalendar doj;

}

package-info

@XmlSchema(namespace="http://java.com/Employee", elementFormDefault=XmlNsForm.QUALIFIED)
@XmlAccessorType(XmlAccessType.FIELD)
package forum7725188;

import javax.xml.bind.annotation.*;

Demo

You can configure the MOXy implementation of Marshaller to output JSON by setting the eclipselink.media-type property to be application/json.

package forum7725188;

import java.io.File;   
import javax.xml.bind.*;
import javax.xml.namespace.QName;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(EmpRequest.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum7725188/input.xml");
        EmpRequest empRequest = (EmpRequest) unmarshaller.unmarshal(xml);

        JAXBElement<EmpRequest> jaxbElement = new JAXBElement<EmpRequest>(new QName(""), EmpRequest.class, empRequest);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.marshal(jaxbElement, System.out);
    }

}

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<EmpRequest xmlns="http://java.com/Employee">
    <EmplIn>
        <EmpID>12</EmpID>
        <Empname>sara</Empname>
        <Designation>SA</Designation>
        <DOJ>2002-05-30T09:30:10+06:00</DOJ>
    </EmplIn>
</EmpRequest>

Output

{"EmplIn" : 
   {"EmpID" : "12",
   "Empname" : "sara",
   "Designation" : "SA",
   "DOJ" : "2002-05-30T09:30:10+06:00"}}

For More Information

这篇关于如何将XmlGregorianCalendar与fromJson和toJson方法一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆