动态JAXB支持将XML转换为JSON
我正在使用EclipseLink(v2.5.0)动态JAXB将XML转换为JSON和viceversa customer.xsd动态JAXB支持将XML转换为JSON,xml,json,jaxb,eclipselink,moxy,Xml,Json,Jaxb,Eclipselink,Moxy,我正在使用EclipseLink(v2.5.0)动态JAXB将XML转换为JSON和viceversa 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"/
<?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>
麦可德
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();
}
}
}
我的问题
1.EclipseLink动态JAXB是否像我在上面尝试的那样正式支持XML到JSON和viceversa的转换,因为我没有看到任何这样的例子
2.如何避免上述nullpointer异常,并且仍然将名为“type”的元素定义为架构的一部分?这是虫子吗?有什么解决办法吗?
我编写演示代码只是为了强调我在其他地方遇到的相同问题,在这里我使用多个XML模式,并且需要对JSON转换进行命名空间感知处理
1.EclipseLink动态JAXB是否正式支持XML到JSON和viceversa转换,正如我在上面尝试的那样,因为我没有看到任何转换
这样的例子
的动态JAXB支持与常规JAXB相同的所有功能,包括JSON绑定
2.如何避免上述nullpointer异常,并且仍然将名为“type”的元素定义为架构的一部分?这是虫子吗?是
有什么解决办法吗?我编写演示代码只是为了突出显示
我在使用多个XML模式和
需要对JSON转换进行命名空间感知处理
您可以将代码更改为以下内容:
//JSON->JAVA->XML
JAXBUnmarshaller jsonumarshaller=jaxbContext.createUnmarshaller();
setProperty(UnmarshallerProperties.MEDIA_类型,“application/json”);
//因为JSON文档中没有根节点,所以应该设置此标志
setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT,false);
//因为没有唯一标识所需类的根节点
//在解组方法中提供一个。为了得到一个“班级”
//DynamicEntity您可以执行以下操作:
类customerType=jaxbContext.getDynamicType(“generated.Customer”).getJavaClass();
StreamSource json=新的StreamSource(“src/forum17446153/customer.json”);
JAXBElement myroot=(JAXBElement)jsonumarshaller.unmarshal(json,customerType);
DynamicEntity myResponse=myroot.getValue();
//由于客户类型在XML模式中命名,因此没有
//与类型关联的根元素。这意味着您将需要
//将其封装在JAXBElement实例中以封送处理。,
JAXBElement jaxbElementResponse=新的JAXBElement(新的QName(“客户”),customerType,myResponse);
marshaller.marshall(jaxbElementResponse,System.out);
更改
StreamSource json = new StreamSource("D:\\GUI\\customer.json");
到
它应该会起作用 谢谢blaise,我在这篇新帖子中更详细地解释了我们面临的问题
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)
StreamSource json = new StreamSource("D:\\GUI\\customer.json");
File json = new File("D:\\GUI\\customer.json");