我正在使用 eclipse link(v2.5.0) Dynamic JAXB 将 XML 转换为 JSON,反之亦然。
客户.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>
客户.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 链接动态 JAXB 是否正式支持 XML 到 JSON 以及我上面尝试过的反之亦然转换,因为我看不到任何这样的例子?
2.如何避免上述空指针异常并且仍然将名为“type”的元素定义为架构的一部分?这是一个错误吗?有什么解决方法吗?我编写演示代码只是为了突出我在其他地方遇到的相同问题,我使用多个 XML 模式并需要命名空间感知处理 JSON 转换。