看了下CSDN博客,关于工具类的大多为下载需要C币,本工具类xml转成json的大部分xml格式都适用。
public class XmlToJsonUtils {
private static Log log = LogFactory.getLog(Xml2JsonUtils.class);
public static String xml2json(String xml) {
//读取Xml文件
StringReader input = new StringReader(xml);
StringWriter output = new StringWriter();
//开启自动配置
JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).autoPrimitive(true).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();
}
}
//json特殊格式 需要去除特殊符号,并保证源xml文件能符合要求 否则转换失败
return output.toString().replace("@", "").replace("#", "").replace("\"{", "{").replace("}\"", "}").replace("\\", "");
}
}
demo测试
public static void main(String[] args) throws Exception {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
String text = FileUtils.readFileToString(new File("F:\\pom.xml"), "UTF-8");
System.out.println(Xml2JsonUtils.xmlTojson(text));
stopWatch.stop();
System.out.println("用时"+stopWatch.getTotalTimeMillis()+"ms");
}
控制台输出
{
"project" : {
"xmlns" : "http://maven.apache.org/POM/4.0.0",
"xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation" : "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd",
"modelVersion" : "4.0.0",
"groupId" : "com.kk",
"artifactId" : "kk",
"version" : "0.0.1-SNAPSHOT",
"packaging" : "war",
"build" : {
"plugins" : {
"plugin" : {
"groupId" : "org.apache.maven.plugins",
"artifactId" : "maven-compiler-plugin",
"version" : "3.5.1",
"configuration" : {
"source" : 1.8,
"target" : 1.8,
"showWarnings" : true
}
}
},
"resources" : {
"resource" : [ {
"directory" : "src/main/resources",
"excludes" : {
"exclude" : [ "**/*.properties", "**/*.xml" ]
},
"filtering" : false
}, {
"directory" : "src/main/java",
"includes" : {
"include" : [ "**/*.properties", "**/*.xml" ]
},
"filtering" : false
}, {
"directory" : "src/main/resources",
"includes" : {
"include" : [ "*.properties", "*.xml" ]
},
"filtering" : false
}, {
"directory" : "${project.basedir}/lib",
"targetPath" : "WEB-INF/lib",
"filtering" : true,
"includes" : {
"include" : "**/*.jar"
}
} ]
}
},
"properties" : {
"spring.version" : "4.3.3.RELEASE",
"mybatis.version" : "3.4.0",
"slf4j.version" : "1.7.7",
"log4j.version" : "1.2.17"
},
"dependencies" : {
"dependency" : [ {
"groupId" : "aopalliance",
"artifactId" : "aopalliance",
"version" : 1.0
}, {
"groupId" : "de.odysseus.staxon",
"artifactId" : "staxon",
"version" : 1.3
}, {
"groupId" : "xom",
"artifactId" : "xom",
"version" : "1.2.5"
}, {
"groupId" : "org.springframework.security",
"artifactId" : "spring-security-web",
"version" : "4.1.3.RELEASE"
}, {
"groupId" : "org.springframework.security",
"artifactId" : "spring-security-config",
"version" : "4.1.3.RELEASE"
}, {
"groupId" : "redis.clients",
"artifactId" : "jedis",
"version" : "2.9.0"
}, {
"groupId" : "org.springframework.data",
"artifactId" : "spring-data-redis",
"version" : "1.8.7.RELEASE"
}, {
"groupId" : "org.springframework.session",
"artifactId" : "spring-session",
"version" : "1.3.1.RELEASE"
}]
}
}
}
用时261ms
基本上大部分xml格式是支持的