1、项目的build.gradle添加 maven { url “https://jitpack.io” }
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" } // add this line
}
}
2、app 的build.gradle添加
implementation 'com.github.smart-fun:XmlToJson:1.4.5' // add this line
3、xml转json使用:
a、转换assets文件夹下面的student_xml.xml文件中的xml格式数据成json数据
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><student><name>cherry</name><id>1</id><age>666</age><desc><state>777</state><items><isTure>ture</isTure></items></desc></student>
public String xml2Json(String fileName) throws IOException {
AssetManager manager = getResources().getAssets();
InputStream input = manager.open(fileName);
int length = input.available();
byte[] bytes = new byte[length];
input.read(bytes);
XmlToJson xmlToJson = new XmlToJson.Builder(new String(bytes)).build();
System.out.println("正常json数据:\n" + xmlToJson.toString());
System.out.println("格式化json数据:\n" + xmlToJson.toFormattedString());
System.out.println("\n" + xmlToJson.toFormattedString("\t"));//要使用的缩进,例如“”或“\ t”。
return "xml2Json----------------End";
}
b、转换assets文件夹下面的student_json.json文件中的json格式数据成xml数据
{"student":{"name":"cherry","id":1,"age":666,"desc":{"state":777,"items":{"isTure":"ture"}}}}
public String json2Xml(String fileName)throws IOException {
AssetManager manager = getResources().getAssets();
InputStream input = manager.open(fileName);
int length = input.available();
byte[] bytes = new byte[length];
input.read(bytes);
JsonToXml jsonToXml = new JsonToXml.Builder(new String(bytes)).build();
String xmlString = jsonToXml.toString();
System.out.println("正常xml数据:\n" + xmlString);
return "json2Xml------------------End";
}