动态JAXB支持将XML转换为JSON

时间:2013-07-03 10:56:40

标签: xml json jaxb eclipselink moxy

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

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

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

{
   "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,反之亦然,正如我上面所尝试的那样,因为我看不到任何这样的例子?

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

2 个答案:

答案 0 :(得分:0)

  

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

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

  

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

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

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

答案 1 :(得分:0)

更改

StreamSource json = new StreamSource("D:\\GUI\\customer.json");

File json = new File("D:\\GUI\\customer.json");

它应该有效。 :)