需求:策略系统要求我们要传入XML格式的字符串,项目接受一个JSON。由于这个XML的格式有点不像常规的,不是所有的属性都是键值对,即下图:
记录下自己做这个的经历,以便以后回想
JSON转JAVA对象
大致格式如下:
所以我们创建对应的实体类,因为最终的XML是China,所以创建一个China类,并且将属性Name以及4个自定义类按层级关系创建好
China
一级:Beijing
一级: Shenzen
二级: Nanshan
二级:Futian
在属性上添加注解@JSONField(name = "JSON中的名字")
顺便重写toString,每个自定定义类都是
public static void main(String[] args) throws JAXBException, IOException, DocumentException { //json解析成对象-由于JSON写成String比较麻烦,先转成Base64实际用再解码 String str = "eyJOYW1lIjoi5Lit5Zu9IiwiQmVpamluZyI6eyJOYW1lIjoi5YyX5LqsIn0sIlNoZW56aGVuIjp7Ik5hbWUiOiLmt7HlnLMiLCJOYW5zaGFuIjp7Ik5hbWUiOiLljZflsbEiLCJQcmljZSI6MTIzLCJEYXRlIjoiMjAxOC0wNC0yOCJ9LCJGdXRpYW4iOnsiTmFtZSI6Iuemj+eUsCJ9fX0=";
str = Base64Coder.decodeString(str);
// 调用Google的FastJson,很简单的就转化成功了。 China china = JSON.toJavaObject(JSON.parseObject(str), China.class); System.out.println(china.toString()); }
对象转XML
首先在每个自定义类上添加以下两个注解
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "转换的XML名字")
然后对单独的每个属性添加注解,在上面的JSON注解下即可,如果是节点:
@XmlElement(name = "节点名")
如果是属性:
@XmlAttribute(name = "属性名")
有些特殊的格式,如Date,可以设定XML的格式。比如需求中我只需要Date不需要时分秒。所以在对应的Date属性上添加:
@XmlSchemaType(name = "date")
回到方法,使用JDK自带的包进行解析,添加一个方法,传入实体类返回字符串
/** * @Title: beanToXml * @Description: JAVA对象转成XML字符串 * @param application * @throws JAXBException * @throws PropertyException 参数说明 * @return void 返回类型 */ public static String beanToXml(Object object) throws JAXBException { JAXBContext jc = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jc.createMarshaller(); // 是否格式化输出 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false); // 设置编码格式 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // 是否不显示头部信息 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter writer = new StringWriter(); marshaller.marshal(object, writer); // 移除date中的时区信息 return writer.toString().replaceAll("\\+08:00", ""); }
正常到这一步差不多了,不过头部其实没有东西显示,而且还有空节点用的是</>
网上有很多类似的例子,不过都有各种不满意的,其实解决方案很简单,就是再转一次!
去除空节点
/** * @Title: formatXmlEmptyElements * @Description: 格式化XML字符串的空节点,<example/>转为<example></example> * @param xml * @return * @throws IOException * @throws DocumentException 参数说明 * @return String 返回类型 */ public static String formatXmlEmptyElements(String xml) throws IOException, DocumentException { OutputFormat xmlFormat = OutputFormat.createPrettyPrint(); // 设置扩展空节点 xmlFormat.setExpandEmptyElements(true); StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, xmlFormat); writer.write(DocumentHelper.parseText(xml)); return out.toString(); }
放上完整的main方法
public static void main(String[] args) throws JAXBException, IOException, DocumentException { //json解析成对象 String str = "eyJOYW1lIjoi5Lit5Zu9IiwiQmVpamluZyI6eyJOYW1lIjoi5YyX5LqsIn0sIlNoZW56aGVuIjp7Ik5hbWUiOiLmt7HlnLMiLCJOYW5zaGFuIjp7Ik5hbWUiOiLljZflsbEiLCJQcmljZSI6MTIzLCJEYXRlIjoiMjAxOC0wNC0yOCJ9LCJGdXRpYW4iOnsiTmFtZSI6Iuemj+eUsCJ9fX0="; str = Base64Coder.decodeString(str); China china = JSON.toJavaObject(JSON.parseObject(str), China.class); System.out.println(china.toString()); //对象解析成XML格式 String xmlString = beanToXml(china); System.out.println(xmlString); //添加尾部空白节点 System.out.println(formatXmlEmptyElements(xmlString)); }
结果: