1
2
3
4
5
6
|
/*
* 解析XML的三种方法:
* 1.SAX 事件驱动 不是一次性加载到内存
* 2.DOM 一次加载到内存,性能较差 适合服务器端编程
* 3.XmlPull 适应嵌入式设备,占用内存较小 API相对简单
*/
|
Android:解析XML的三种方法XmlPull、SAX、DOM一、XMLpull()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
private
void
XMLpull()
//根据标签一个一个判断,是找到对应索引的数据,否判断下一个标签
{
try
{
AssetManager assets = getAssets();
InputStream is = assets.open(
"testxml.xml"
);
XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(is,
"utf-8"
);
int
eventType = pullParser.getEventType();
//得到节点
while
(eventType != XmlPullParser.END_DOCUMENT)
//XML是否读取结束
{
if
(eventType == XmlPullParser.START_TAG)
//是否标签开始
{
String name = pullParser.getName();
if
(
"TextView"
.equals(name))
{
String string = pullParser.getAttributeName(
3
);
//根据索引位置读取数据
Toast.makeText(
this
,
"TextView是:"
+ string, Toast.LENGTH_LONG).show();
}
}
eventType = pullParser.next();
//进入下一个标签
}
}
catch
(IOException e)
{
e.printStackTrace();
}
catch
(XmlPullParserException e)
{
e.printStackTrace();
}
}
|
其他方法:
1.SAX:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
private
void
btnSAX()
{
SAXxml(
"testxml.xml"
,
"Title"
,
"android:text"
);
}
private
void
SAXxml(String FileName,
final
String label,
final
String attr)
{
try
{
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
AssetManager asset = getAssets();
InputStream is = asset.open(FileName);
//找到asset的xml文件
DefaultHandler dh =
new
DefaultHandler()
{
@Override
public
void
startElement(String uri, String localName,
String qName, Attributes attributes)
throws
SAXException
{
if
(label.equals(localName))
//找到标签
{
String value = attributes.getValue(attr);
//找到key
mTextView.setText(value);
}
super
.startElement(uri, localName, qName, attributes);
}
};
parser.parse(is, dh);
}
catch
(ParserConfigurationException e)
{
e.printStackTrace();
}
catch
(SAXException e)
{
e.printStackTrace();
}
catch
(IOException e)
{
e.printStackTrace();
}
}
|
2.dom:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
protected
void
dom()
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
AssetManager assets = getAssets();
InputStream is = assets.open(
"activity_main.xml"
);
Document douDocument = builder.parse(is);
//解析所有内容到容器内
NodeList root = douDocument.getChildNodes();
for
(
int
i =
0
; i < root.getLength(); i++)
//三层结构,根据xml文件结构确定
{
Node node = root.item(i);
NodeList nodes = node.getChildNodes();
for
(
int
j =
0
; j < nodes.getLength(); j++)
{
Node item = nodes.item(j);
if
(
"LinearLayout"
.equals(item.getNodeName()))
{
NodeList nodes2 = item.getChildNodes();
for
(
int
k =
0
; k < nodes2.getLength(); k++)
{
Node item2 = nodes2.item(k);
if
(
"Button"
.equals(item2.getNodeName()))
{
Element ele = (Element) item2;
String width = ele
.getAttribute(
"android:layout_width"
);
Toast.makeText(MainActivity.
this
,
"Button宽度是: "
+ width,
Toast.LENGTH_LONG).show();
}
}
}
}
}
}
catch
(ParserConfigurationException e)
{
e.printStackTrace();
}
catch
(SAXException e)
{
e.printStackTrace();
}
catch
(IOException e)
{
e.printStackTrace();
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1212828,如需转载请自行联系原作者