您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Java中怎么將xml轉換為Json,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
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
上述就是小編為大家分享的Java中怎么將xml轉換為Json了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。