【问题标题】:Dynamic JAXB support to convert XML to JSON动态 JAXB 支持将 XML 转换为 JSON
【发布时间】:2013-07-03 10:56:39
【问题描述】:

我正在使用 eclipse link(v2.5.0) Dynamic 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>

客户.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"
   }
}

我的代码

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 链接 Dynamic JAXB 是否正式支持 XML 到 JSON 以及我上面尝试过的反之亦然的转换,因为我看不到任何这样的例子?

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

【问题讨论】:

    标签: xml json jaxb eclipselink moxy


    【解决方案1】:

    1. Eclipse 链接动态 JAXB 是否正式支持 XML 到 JSON 以及我上面尝试过的反之亦然转换,因为我看不到任何 这样的例子?

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

    2.如何避免上述空指针异常并且仍然​​将名为“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);
    

    【讨论】:

    【解决方案2】:

    改变

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

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

    它应该可以工作。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 2019-08-03
      相关资源
      最近更新 更多