此项目通过简单的天气情况显示来实现xml文件的解析
1. 搭建环境
1)创建如下图的包、类及文件
2)布局文件的大概样式如下图所示,用到四个TextView控件和一个RadioGroup控件
3)china.xml文件的内容如下
<?xml version="1.0" encoding="UTF-8"?>
<china dn="day">
<city
cityname="南京"
pyName="jiangsu"
quName="江苏"
state1="1"
state2="1"
stateDetailed="多云"
tem1="24"
tem2="19"
windState="西北风3-4级" />
<city
cityname="北京"
pyName="beijing"
quName="北京"
state1="1"
state2="1"
stateDetailed="多云"
tem1="26"
tem2="15"
windState="西北风5-6级" />
</china>
4)City类中主要就是按照china.xml文件中<city>标签内的属性声明变量,并实现无参和有参构造器,以及各自的get和set方法,有需要的话可以实现toString方法。
2. DOMXML的主要代码如下,可通过注释加强理解:
public class DOMXML {
public List<City> domXml() {
// 创建返回的集合对象
List<City> cities = new ArrayList<City>();
// 创建文档解析器的工厂对象
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
try {
// 得到文档解析器对象
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// 获取输入流对象
InputStream is = getClass().getClassLoader().getResourceAsStream(
"china.xml");
// 通过解析器的parse方法解析流对象,转化成文档对象
Document document = builder.parse(is);
// 返回的是文档的根节点
Element rootElement = document.getDocumentElement();
// 获取指定属性的属性值
// String value = rootElement.getAttribute("dn");
// 判断是否有孩子节点
if (rootElement.hasChildNodes()) {
// 获取所有子节点
NodeList nodeList = rootElement.getChildNodes();
// 遍历子节点
for (int i = 0; i < nodeList.getLength(); i++) {
// 获取子节点对象
Node node = nodeList.item(i);
// 判断子节点的类型---文本节点 元素节点
if (node.getNodeType() == Element.ELEMENT_NODE) {
// 创建一个实体对象,保存元素节点值
City city = new City();
// 获取元素节点的属性节点
// 转换成元素节点
Element element = (Element) node;
// 得到这个节点中的所有属性
NamedNodeMap map = element.getAttributes();
// 遍历所有属性节点
for (int j = 0; j < map.getLength(); j++) {
// 获取具体某个属性节点
Attr attr = (Attr) map.item(j);
// 获取属性的值和属性名
if ("cityname".equals(attr.getNodeName())) {
city.setCityName(attr.getNodeValue());
} else if ("pyName".equals(attr.getNodeName())) {
city.setPyName(attr.getNodeValue());
} else if ("quName".equals(attr.getNodeName())) {
city.setQuName(attr.getNodeValue());
} else if ("state1".equals(attr.getNodeName())) {
city.setState1(attr.getNodeValue());
} else if ("state2".equals(attr.getNodeName())) {
city.setState2(attr.getNodeValue());
} else if ("stateDetailed".equals(attr
.getNodeName())) {
city.setStateDetailed(attr.getNodeValue());
} else if ("tem1".equals(attr.getNodeName())) {
city.setTem1(attr.getNodeValue());
} else if ("tem2".equals(attr.getNodeName())) {
city.setTem2(attr.getNodeValue());
} else if ("windState".equals(attr.getNodeName())) {
city.setWindState(attr.getNodeValue());
}
}
// 添加到集合中
cities.add(city);
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cities;
}
public class MainActivity extends Activity implements OnCheckedChangeListener {
private RadioGroup rg;
private TextView tv_wendu, tv_fengli;
private DOMXML domxml;
private List<City> cities;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg = (RadioGroup) findViewById(R.id.rg);
// 注册监听事件
rg.setOnCheckedChangeListener(this);
tv_wendu = (TextView) findViewById(R.id.tv_wendu);
tv_fengli = (TextView) findViewById(R.id.tv_fengli);
// 解析xml文件对象的实例化
domxml = new DOMXML();
// 调用相应的方法
cities = domxml.domXml();
//如何显示缺省值
int checkedId = rg.getCheckedRadioButtonId();
initData(checkedId);
}
// checkedId就是被选中的id值
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
initData(checkedId);
}
//根据选中的id显示不同城市的天气
private void initData(int checkedId) {
City city = null;
switch (checkedId) {
case R.id.radio0:
for (City c : cities) {
if ("北京".equals(c.getCityName())) {
city = c;
}
}
break;
case R.id.radio1:
for (City c : cities) {
if ("南京".equals(c.getCityName())) {
city = c;
}
}
break;
}
if (city != null) {
tv_wendu.setText(city.getTem2() + "°~" + city.getTem1() + "°");
tv_fengli.setText(city.getWindState());
}
}
}
4. 主要功能:
通过点击北京/南京的按钮进行切换,温度和风力随之改变。
5. 备注:
在判断子节点的类型之后,可以不创建实体对象,在转换成元素节点后直接添加到集合中:
Element element = (Element) node;
// 把转换的city对象添加到集合中
cities.add(new City(element.getAttribute("cityname"),
element.getAttribute("pyName"),
element.getAttribute("quName"),
element.getAttribute("state1"),
element.getAttribute("state2"),
element.getAttribute("stateDetailed"),
element.getAttribute("tem1"),
element.getAttribute("tem2"),
element.getAttribute("windState")));