android json转换xml格式,Android如何把json格式的数据存储到xml中

这段代码从资源中读取JSON文件,将其内容解析为字符串,然后利用JSON库处理数据,提取出'provence','city'和'area'的数组。接着,使用XML序列化对象将这些数据转换为XML格式,并分别写入到'provinces.xml','cities.xml'和'districts.xml'三个本地文件中。整个过程涉及文件操作、数据解析和XML构建。
摘要由CSDN通过智能技术生成

private void getAddress(){

Log.e("获取地址",".......");

/*StringBuilder sb = new StringBuilder();

AssetManager am = activity.getAssets();

try {

BufferedReader br = new BufferedReader(new InputStreamReader(

am.open("pro")));

String next = "";

while (null != (next = br.readLine())) {

sb.append(next);

}

} catch (IOException e) {

TODO Auto-generated catch block

e.printStackTrace();

sb.delete(0, sb.length());

}*/

将json文件读取到buffer数组中

InputStream is = this.getResources().openRawResource(R.raw.pro);

byte[] buffer = new byte[0];

try {

buffer = new byte[is.available()];

is.read(buffer);

} catch (IOException e) {

e.printStackTrace();

}

将字节数组转换为以utf-8编码的字符串

String json = null;

try {

json = new String(buffer, "utf-8");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

Log.e("sb",json);

Log.e("sb",sb.toString());

JSONObject object = JsonUtil.convertJsonObj(json);

JSONObject object1=JsonUtil.getJsonObj(object,"data");

JSONArray JSONArrayprovence = JsonUtil.convertJsonArry(object1, "provence");

JSONArray JSONArraycity = JsonUtil.convertJsonArry(object1, "city");

JSONArray JSONArrayarea = JsonUtil.convertJsonArry(object1, "area");

Log.e("333", JSONArrayprovence.length() + "");

Log.e("11", JSONArrayarea.length() + "");

Log.e("222", JSONArraycity.length() + "");

XmlSerializer serializer = Xml.newSerializer();获取XML写入信息的序列化对象

File file = new File(Environment.getExternalStorageDirectory(), "provinces.xml");

try {

FileOutputStream outStream = new FileOutputStream(file,true);

serializer.setOutput(outStream, "UTF-8");

serializer.startDocument("UTF-8", true);

serializer.startTag("", "province");

for (int i = 0; i < JSONArrayprovence.length(); i++) {

JSONObject jsonObject=JsonUtil.convertJsonObj(JSONArrayprovence, i);

try {

serializer.startTag("", "Province");

serializer.attribute("", "provinceID", JsonUtil.getStr(jsonObject, "provinceID"));

serializer.attribute("", "province", JsonUtil.getStr(jsonObject, "province"));

serializer.attribute("", "father", JsonUtil.getStr(jsonObject, "father"));

serializer.text(JsonUtil.getStr(jsonObject, "province"));

serializer.endTag("", "Province");

} catch (IOException e) {

e.printStackTrace();

}

}

serializer.endTag("", "province");

serializer.endDocument();

serializer.flush();

outStream.close();

} catch (IOException e) {

e.printStackTrace();

}

XmlSerializer serializercity = Xml.newSerializer();获取XML写入信息的序列化对象

File file1 = new File(Environment.getExternalStorageDirectory(), "cities.xml");

try {

FileOutputStream outStream = new FileOutputStream(file1,true);

serializercity.setOutput(outStream, "UTF-8");

serializercity.startDocument("UTF-8", true);

serializercity.startTag("", "city");

for (int i = 0; i < JSONArraycity.length(); i++) {

JSONObject jsonObject=JsonUtil.convertJsonObj(JSONArraycity, i);

try {

serializercity.startTag("", "City");

serializercity.attribute("", "city",JsonUtil.getStr(jsonObject, "city"));

serializercity.attribute("", "cityID", JsonUtil.getStr(jsonObject, "cityID"));

serializercity.attribute("", "father", JsonUtil.getStr(jsonObject, "father"));

serializercity.text(JsonUtil.getStr(jsonObject, "city"));

serializercity.endTag("", "City");

} catch (IOException e) {

e.printStackTrace();

}

}

serializercity.endTag("", "city");

serializercity.endDocument();

serializercity.flush();

outStream.close();

} catch (IOException e) {

e.printStackTrace();

}

XmlSerializer serializerarea = Xml.newSerializer();获取XML写入信息的序列化对象

File file2 = new File(Environment.getExternalStorageDirectory(), "districts.xml");

try {

FileOutputStream outStream = new FileOutputStream(file2,true);

serializerarea.setOutput(outStream, "UTF-8");

serializerarea.startDocument("UTF-8", true);

serializerarea.startTag("", "district");

for (int i = 0; i < JSONArrayarea.length(); i++) {

JSONObject jsonObject= JsonUtil.convertJsonObj(JSONArrayarea, i);

try {

serializerarea.startTag("", "District");

serializerarea.attribute("", "area", JsonUtil.getStr(jsonObject,"area"));

serializerarea.attribute("", "areaID", JsonUtil.getStr(jsonObject, "areaID"));

serializerarea.attribute("", "father", JsonUtil.getStr(jsonObject, "father"));

serializerarea.text(JsonUtil.getStr(jsonObject, "area"));

serializerarea.endTag("", "District");

} catch (IOException e) {

e.printStackTrace();

}

}

serializerarea.endTag("", "district");

serializerarea.endDocument();

serializerarea.flush();

outStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值