动态JAXB支持将XML转换为JSON [英] Dynamic JAXB support to convert XML to JSON

查看:83
本文介绍了动态JAXB支持将XML转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用eclipse链接(v2.5.0)动态JAXB将XML转换为JSON,反之亦然。

I am using eclipse link(v2.5.0) Dynamic JAXB to convert XML to JSON and viceversa.

customer.xsd

customer.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="address" type="address"/>
  <xs:element name="customer" type="customer"/>

  <xs:complexType name="address">
    <xs:sequence>
      <xs:element name="city" type="xs:string" minOccurs="0"/>
      <xs:element name="street" type="xs:string" minOccurs="0"/>
       <xs:element name="type" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="customer">
    <xs:sequence>
      <xs:element ref="address" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

customer.xml

customer.xml

<?xml version="1.0" encoding="UTF-8"?>
<customer>
   <name>Jane Doe</name>
   <address>
      <city>My Town</city>
      <street>123 Any Street</street>
      <type>work</type>
    </address>

</customer>

customer.json

customer.json

{
   "address" : {
      "city" : "My Town",
      "street" : "123 Any Street",
      "type" : "work"
   }
}

MyCode

public class Demo {

    public static void main(String[] args) {
         try {

                // create DynamicJAXBContext
                FileInputStream xsdInputStream = new FileInputStream("D:\\GUI\\customer.xsd");
                DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);

                // Unmarshal XML--> Java
                FileInputStream xmlInputStream = new FileInputStream("D:\\GUI\\customer.xml");
                JAXBUnmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                JAXBElement<DynamicEntity> root = (JAXBElement)unmarshaller.unmarshal(xmlInputStream);
                JAXBMarshaller marshaller = jaxbContext.createMarshaller();
                DynamicEntity javaResponse = root.getValue();

                Map namespaces = new HashMap();             
                // Marshal Java --> JSON
                JAXBMarshaller jsonMarshaller = jaxbContext.createMarshaller();
                jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
                jsonMarshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces);
                FileOutputStream jsonOutputStream = new FileOutputStream("D:\\GUI\\customer.json");
                jsonMarshaller.marshal(javaResponse, jsonOutputStream);

                // JSON->JAVA->XML
                JAXBUnmarshaller jsonUnmarshaller = jaxbContext.createUnmarshaller();
                jsonUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
                jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespaces);
                StreamSource json = new StreamSource("D:\\GUI\\customer.json");
                JAXBElement<DynamicEntity> myroot = (JAXBElement)jsonUnmarshaller.unmarshal(json);
                DynamicEntity myResponse = myroot.getValue();

                marshaller.marshal(myResponse, System.out);

            } catch (JAXBException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } 

        }

}

异常

Exception in thread "main" java.lang.NullPointerException
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:264)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:443)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:296)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parseRoot(JSONReader.java:166)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:125)
    at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:140)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:778)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:666)
    at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:593)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:287)
    at Demo.main(Demo.java:47)

我的问题

1.是否eclipse链接动态JAXB正式支持XML到JSON,反之亦然,正如我上面所尝试的那样,因为我看不到任何这样的例子?

1.Does eclipse link Dynamic JAXB officially support XML to JSON and viceversa conversions as I have tried above, as I could not see any such examples?

2 。如何避免上述nullpointer异常,并且仍然有一个名为type的元素被定义为模式的一部分?这是一个错误吗?有没有解决方法?
我编写的演示代码只是为了突出我在其他地方遇到的同样问题,我使用多个XML模式并需要一个名称空间感知处理JSON转换。

2.How to avoid the above nullpointer exception and still have an element named "type" defined as part of the schema? Is this a bug? Are there any workarounds? I have written the demo code only to highlight the same problem I face elsewhere where I use multiple XML schemas and require a namespace aware handling for JSON conversion.

推荐答案


1.Eclipse链接动态JAXB正式支持XML到JSON,反之亦然,正如我上面所尝试的那样,因为我看不到任何
这样的例子?

1.Does eclipse link Dynamic JAXB officially support XML to JSON and viceversa conversions as I have tried above, as I could not see any such examples?

EclipseLink JAXB(MOXy) 动态JAXB支持与常规JAXB相同的所有功能,包括JSON绑定。

EclipseLink JAXB (MOXy)'s Dynamic JAXB supports all of the same features as its regular JAXB including JSON-binding.


2.如何避免上述nullpointer异常,并且仍然有一个名为type的元素被定义为模式的一部分?这是一个错误吗?
有任何变通方法吗?我编写了演示代码只是为了突出显示
我在其他地方遇到的同样问题,我使用多个XML模式,
需要一个名称空间感知处理JSON转换。

2.How to avoid the above nullpointer exception and still have an element named "type" defined as part of the schema? Is this a bug? Are there any workarounds? I have written the demo code only to highlight the same problem I face elsewhere where I use multiple XML schemas and require a namespace aware handling for JSON conversion.

您可以将代码更改为以下内容:

You could change your code to the following:

            // JSON->JAVA->XML
            JAXBUnmarshaller jsonUnmarshaller = jaxbContext.createUnmarshaller();
            jsonUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");

            // Since there is no root node in your JSON document you should set this flag
            jsonUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);

            // Since there is no root node to uniquely identify the class you need 
            // to supply one in the unmarshal method.  To get the "class" for a 
            // DynamicEntity you can do the following:
            Class customerType = jaxbContext.getDynamicType("generated.Customer").getJavaClass();
            StreamSource json = new StreamSource("src/forum17446153/customer.json");
            JAXBElement<DynamicEntity> myroot = (JAXBElement)jsonUnmarshaller.unmarshal(json, customerType);
            DynamicEntity myResponse = myroot.getValue();

            // Since the customer type is named in the XML schema there isn't
            // a root element associated with the type.  This means you will need
            // to wrap in in an instance of JAXBElement to marshal it.,
            JAXBElement jaxbElementResponse = new JAXBElement(new QName("customer"), customerType, myResponse);
            marshaller.marshal(jaxbElementResponse, System.out);

这篇关于动态JAXB支持将XML转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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