把XML文件内容转成JSON串

public static String ConvertXMLtoJSON("f:/test.xml"){

// 获取xml字符串

String xml = getXMLString(filePath);

// 序列化

XMLSerializer xmlSerializer = new XMLSerializer();

// 把xml内容转成json

JSON json = xmlSerializer.read(xml);

return json.toString(2);

}


// 获取xml字符串

private static String getXMLString(String filePath) {

StringBuffer sb = new StringBuffer();

BufferedReader br = null;

String line;

try {

InputStreamReader isr = new InputStreamReader(new FileInputStream(

filePath), "UTF-8");

br = new BufferedReader(isr);

while (true) {

line = br.readLine();

if (line == null) {

break;

}

sb.append(line + "\n");

}

} catch (Exception e) {


e.printStackTrace();

}

return sb.toString();

}

你可能感兴趣的:(把XML文件内容转成JSON串)