一 运用
1. maven 导入
<dependency>
<groupId>de.odysseus.staxon</groupId>
<artifactId>staxon</artifactId>
<version>1.3</version>
</dependency>
2. 使用配置
public class StaxonUtils {
/**
* @Description: json string 转 xml string
*/
public static String json2xmlPay(String json) {
StringReader input = new StringReader(json);
StringWriter output = new StringWriter();
JsonXMLConfig config = new JsonXMLConfigBuilder().multiplePI(false).repairingNamespaces(false).build();
try {
XMLEventReader reader = new JsonXMLInputFactory(config).createXMLEventReader(input);
XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(output);
writer = new PrettyXMLEventWriter(writer);
writer.add(reader);
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 封装xml
return xmlHeaderBuild(output);
}
/**
* 头部 xml 不会自动生成,手动拼接上去
*
* @param output
* @return
*/
static String xmlHeaderBuild(StringWriter output) {
if (output.toString().length() >= 38) {
return "<?xml version=\"1.0\" encoding=\"gbk\"?>\n"
+ output.toString().substring(38);
}
return output.toString();
}
/**
* @Description: xml string 转 json string
* @author watermelon_code
* @date 2017年7月19日 上午10:50:46
*/
public static String xml2json(String xml) {
StringReader input = new StringReader(xml);
StringWriter output = new StringWriter();
JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).autoPrimitive(false).prettyPrint(true).build();
try {
XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(input);
XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(output);
writer.add(reader);
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return output.toString();
}
public static void main(String[] args) {
String tests = " <ywxx>\n" +
" <fplxdm>31210000E476929748</fplxdm>\n" +
" <fpxx count=\"1\">\n" +
" <group xh=\"1\">\n" +
" <fpdm>0320022002xx</fpdm>\n" +
" <bmbbbh>42.0</bmbbbh>\n" +
" <hsslbs>0</hsslbs>\n" +
" <qdbz>1</qdbz>\n" +
" <fyxm count=\"1\">\n" +
" <group xh=\"1\">\n" +
" <fphxz>3</fphxz>\n" +
" <spmc>(详见销货清单)</spmc>\n" +
" <je>111.49</je>\n" +
" <sl>0.13</sl>\n" +
" <se>14.51</se>\n" +
" </group>\n" +
" </fyxm>\n" +
" <qdxm count=\"2\">\n" +
" <group xh=\"1\">\n" +
" <fphxz>0</fphxz>\n" +
" <spmc>*食品制造机械*30CM锯刀</spmc>\n" +
" <je>15.93</je>\n" +
" <sl>0.13</sl>\n" +
" <se>2.07</se>\n" +
" </group>\n" +
" <group xh=\"2\">\n" +
" <fphxz>0</fphxz>\n" +
" <spmc>*食品制造机械*30CM锯刀</spmc>\n" +
" <je>15.04</je>\n" +
" <sl>0.13</sl>\n" +
" <se>1.96</se>\n" +
" </group>\n" +
" </qdxm>\n" +
" <hjje>111.49</hjje>\n" +
" </group>\n" +
" </fpxx>\n" +
" </ywxx>";
String s = xml2json(tests);
System.out.println(s);
}
}
二 属性介绍和坑
在 xml 转json 中,使用 xml2json 方法,核心配置为 jsonXMLConfig,目前了解的配置有
1. autoArray() 是否自动添加 JSON 数组边界(默认为false)。自动将xml 中的数组节点 转为 json数组,如果为false,则xml转换为字符串时,group 节点会并列,导致转json时,只会取最后一个节点,如下图所示
2. autoPrimitive() 是否自动将元素文本转换为 JSON 原语(默认为false)。为true时自动将xml值转为对应类型,直观的比如纯数字值就不会加双引号,为false 自动会将所有值转为字符串。这里有个坑就是 为true时,有些值他会自动转换,比如这里的 31210000E476929748,会转为 3.1210000E+476929755,变成科学计数法了
3. multiplePI() 是否遵守<? xml-multiple ... ?>处理指令(默认为true)
4. prettyPrint() 是否格式化输出以提高可读性 (默认为false)
5. namespaceSeparator() 用作命名空间前缀分隔符的字符(默认为’:')