在上篇介绍了json解析,这是服务器常用的返回方式,里面huiyou json数组的情况,我们要灵活的定义集合,内部类等方式破解之。链接:
http://blog.csdn.net/sinat_24235127/article/details/79092453
本文结合下项目中的例子,介绍下xml解析
关于xml和json的区别,可以参考这篇博文:https://www.cnblogs.com/SanMaoSpace/p/3139186.html
主要是json解析简单。xml更加容易看懂,和人类语言更加接近,webservice里xml的地位不可撼动。
问:什么情况下使用xml格式:
在项目里有这种需求,后台修改App的配置文件后,app这边能体现出来。比如在配置文件里加入几条数据(这里一般不是软件开发人员,可能是硬件工程师、c++工程师,或者测试工程师来做),手机里能获取到。但是又不是传统的数据库表操作这种。这时候就可以使用xml这种通俗的格式了。
加入到代码在线解析:http://tool.oschina.net/codeformat/xml
后台配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="DynamicControl" type="WisdomDetectionTool.DynamicControl.Models.DynamicControlModel, WisdomDetectionTool"/>
</configSections>
<appVersion name="app_v1_2_3.apk"></appVersion>
<DynamicControl CheckSuccessText="1" NumberPerPage="10" IsMultiCheck="True">
<ControlGroupList Title="新能源相关配置">
<ControlGroup Title="新能源IP端口" Content1="*8888*91#[IP地址]:[端口]" ButtonText1="设置" Content2="*8888*206#" ButtonText2="查询" Checked="True">
<Controls>
<Control ControlType="TextBox" Name="IP地址" Value="220.178.32.20"/>
<Control ControlType="TextBox" Name="端口" Value="1234"/>
</Controls>
</ControlGroup>
<ControlGroup Title="新能源IP" Content1="*8888*91#[IP地址]" ButtonText1="设置" Content2="*8888*206#" ButtonText2="查询" Checked="True">
<Controls>
<Control ControlType="TextBox" Name="IP地址" Value="220.178.32.20"/>
</Controls>
</ControlGroup>
<ControlGroup Title="新能源端口" Content1="*8888*96#[端口]" ButtonText1="设置" Checked="False">
<Controls>
<Control ControlType="TextBox" Name="端口" Value="4007"/>
</Controls>
</ControlGroup>
<ControlGroup Title="车牌颜色" Content1="*8888*12#[颜色]" ButtonText1="设置" Content2="*8888*204#" ButtonText2="查询" Checked="True">
<Controls>
<Control ControlType="ComboBox" Name="颜色" Value="2" Data="1:蓝色,2:黄色,3:黑色,4:白色,9:其它"/>
</Controls>
</ControlGroup>
<ControlGroup Title="新能源车型" Content1="*8888*101#[车型]" ButtonText1="设置">
<Controls>
<Control ControlType="ComboBox" Name="车型" Value="1" Data="0:无车型,1:混合动力松正,2:混合动力绿控,3:安凯纯电动,4:安凯混动"/>
</Controls>
</ControlGroup>
</ControlGroupList>
<ControlGroupList Title="其它配置">
<ControlGroup Title="查询电话" Content1="*8888*203#" ButtonText1="查询">
<Controls>
<Control ControlType="TextBox" Name="" Value="查询APN、ACC、电话卡状态、网络注册情况" Enable="False"/>
</Controls>
</ControlGroup>
<ControlGroup Title="超级命令" Content1="verwis" ButtonText1="发送">
<Controls>
<Control ControlType="TextBox" Name="" Value=""/>
</Controls>
</ControlGroup>
</ControlGroupList>
</DynamicControl>
</configuration>
对这个数据的解析可以在服务器完成:但是会损害服务器的性能,再加上又要麻烦后台人员,或者以后配置文件的格式出现了变化都是很麻烦的。最好还是在客户端这里做。
只要求服务器把配置文件的内容以字符串的形式返回过来就可以了。
本地在把字符串转成流,最后开始解析:
InputStream is = new ByteArrayInputStream(codeAndMsgBean.getReturnMessage().trim().getBytes());
mS = XmlParseUtil.xmlToBean(is);
Log.d("elliotlin","mS:"+mS);//ms为json格式的数据了
我们来看看这篇XmlParseUtil这个转化的过程是如何做到解析xml和封装json的,毕竟json格式在前端很通用(W3C的规范)
数据结构
package com.hst.mininurse.utils;
import java.util.ArrayList;
/**
* Author:lsh
* Version: 1.0
* Description:
* Date: 2017/8/29
*/
//最外层对象<configuration>
public class Config {
public ConfigSections configSections;
public DynamicControl DynamicControl;
public class ControlGroupList{
// public String Title;
public ArrayList<ControlGroup> controlGroupList;
}
public class ControlGroup {
public String Title;
public String Content1;
public String ButtonText1;
public String Content2;
public String ButtonText2;
public String Checked;
public Controls Controls;
}
public class Controls {
public Control Control;
}
public class Control {
public String ControlType;
public String Name;
public String Value;
public String Data;
public String Enable;
public String Checked;
}
//第二层第一个儿子的儿子
public class Section {
public String name;
public String type;
}
// 第二层第一个儿子 <configSections>
public class ConfigSections {
public Section section;
}
// 第二层第二个儿子 <DynamicControl
public class DynamicControl {
public String CheckSuccessText;
public String NumberPerPage;
public String IsMultiCheck;
public ArrayList ControlGroups;
}
}
解析过程
{
//XmlPullParserFactory首先是获取实例
XmlPullParserFactory factory = null;
Config config = new Config();
try {
factory = XmlPullParserFactory.newInstance();
//利用实例调用setinput将数据写进去
XmlPullParser parser = factory.newPullParser();
// InputStream is = new ByteArrayInputStream(content.getBytes());
parser.setInput(is,"utf-8");
//通过调用此方法得到当前解析事件
int type = parser.getEventType();
Config.DynamicControl dynamicControl = config.new DynamicControl();
ArrayList<ArrayList<Config.ControlGroup>> controlGroupss = new ArrayList<>();//学校
ArrayList<Config.ControlGroup> ControlGroupLists = null;
Config.ConfigSections ConfigSections = null;
Config.ControlGroup controlGroup = null;
Config.Controls controls = null;
Config.Control control = null;
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("configuration".equals(parser.getName())) {
// config = new Config();
} else if ("configSections".equals(parser.getName())) {
ConfigSections = config.new ConfigSections();
} else if ("section".equals(parser.getName())) {
Config.Section section = config.new Section();
String name = parser.getAttributeValue(null, "name");
String typeName = parser.getAttributeValue(null, "type");
section.name = name;
section.type = typeName;
ConfigSections.section = section;
} else if ("DynamicControl".equals(parser.getName())) {
dynamicControl.CheckSuccessText = parser.getAttributeValue(null, "CheckSuccessText");
dynamicControl.NumberPerPage = parser.getAttributeValue(null, "NumberPerPage");
dynamicControl.IsMultiCheck = parser.getAttributeValue(null, "IsMultiCheck");
} else if("ControlGroupList".equals(parser.getName())){
ControlGroupLists=new ArrayList<>();
//controlGroupList.Title=parser.getAttributeValue(null,"Title");
}
else if ("ControlGroup".equals(parser.getName())) {
controlGroup = config.new ControlGroup();
controlGroup.Title = parser.getAttributeValue(null, "Title");
controlGroup.Content1 = parser.getAttributeValue(null, "Content1");
controlGroup.ButtonText1 = parser.getAttributeValue(null, "ButtonText1");
controlGroup.Content2 = parser.getAttributeValue(null, "Content2");
controlGroup.ButtonText2 = parser.getAttributeValue(null, "ButtonText2");
controlGroup.Checked = parser.getAttributeValue(null, "Checked");
// controlGroup.controls = controls;
} else if ("Controls".equals(parser.getName())) {
controls = config.new Controls();
} else if ("Control".equals(parser.getName())) {
control = config.new Control();
control.ControlType = parser.getAttributeValue(null, "ControlType");
control.Name = parser.getAttributeValue(null, "Name");
control.Value = parser.getAttributeValue(null, "Value");
control.Data = parser.getAttributeValue(null, "Data");
control.Enable = parser.getAttributeValue(null, "Enable");
control.Checked = parser.getAttributeValue(null, "Checked");
controls.Control = control;
}else if("appVersion".equals(parser.getName())){
appVersion=parser.getAttributeValue(null,"name");
Log.d("maoxiaoqiang2",appVersion);
}
break;
case XmlPullParser.END_TAG:
if ("configSections".equals(parser.getName())) {
config.configSections = ConfigSections;
} else if ("DynamicControl".equals(parser.getName())) {
dynamicControl.ControlGroups = controlGroupss;
config.DynamicControl = dynamicControl;
}else if("ControlGroupList".equals(parser.getName())){
controlGroupss.add(ControlGroupLists);
}
else if ("ControlGroup".equals(parser.getName())) {
controlGroup.Controls = controls;
ControlGroupLists.add(controlGroup);
} else if ("Controls".equals(parser.getName())) {
controls.Control = control;
} else if ("configuration".equals(parser.getName())) {
}
break;
}
type = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
// Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
// Toast.makeText(this, "IOException", Toast.LENGTH_SHORT).show();
}
String s = new Gson().toJson(config);
return s;
}
最后一行调用的是toJson即为得到Json
整个过程:就是xml-bean-Json