Xml

遍历出xml中的属性

例子1

set.xml

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<accent>
<!-- 配置数据 -->
<ip>192.168.1.104</ip>
<port>0001</port>
<user>2096</user>
<password>2096</password>
</accent>

设置一个Bean类保存信息

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
public class MySetBean {
private String ip;
private String port;
private String user;
private String password;

public MySetBean() {
}

public MySetBean(String ip, String port, String user, String password) {
this.ip = ip;
this.port = port;
this.user = user;
this.password = password;
}

//set/get
//...

@Override
public String toString() {
//...
}

@Override
public boolean equals(Object o) {
//...
}

@Override
public int hashCode() {
//...
}
}

读取xml文件数据

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
48
49
50
public class XMLUtil {
public MySetBean ReadXML(Context context, String sXMLFile) {
MySetBean setBean = new MySetBean();
try {
//1.获得DOM解析器的工厂示例:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//2.从Dom工厂中获得dom解析器
DocumentBuilder builder = dbFactory.newDocumentBuilder();
//3.使用文件的路径+文件名生成File对象
File tempFile = new File(sXMLFile);
//如果存在
if (tempFile.exists()) {
// Toast.makeText(context, "正在读取系统配置文件", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "系统配置文件不存在", Toast.LENGTH_SHORT).show();
return null;
}
FileInputStream inputStream = new FileInputStream(tempFile);
Document document = null;
if (inputStream != null) {
document = builder.parse(inputStream);
}
//4.得到文档中名称为accent的元素的结点,文件中只有一个所以不需要遍历
NodeList nodeList = document.getElementsByTagName("accent").item(0).getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
//读取配置信息
if (nodeList.item(i).getNodeName().equals("ip")) {
setBean.setIp(nodeList.item(i).getFirstChild().getNodeValue().trim());
}
if (nodeList.item(i).getNodeName().equals("port")) {
setBean.setPort(nodeList.item(i).getFirstChild().getNodeValue().trim());
}
if (nodeList.item(i).getNodeName().equals("user")) {
setBean.setUser(nodeList.item(i).getFirstChild().getNodeValue().trim());
}
if (nodeList.item(i).getNodeName().equals("password")) {
setBean.setPwd(nodeList.item(i).getFirstChild().getNodeValue().trim());
}
}
}
//关闭文件
inputStream.close();
tempFile = null;
} catch (Exception e) {
e.printStackTrace();
}
return setBean;
}
}

例子2

xmlfile.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<Languages cat="it">
<lan id="1">
<name>Java</name>
<ide>Eclipse</ide>
<type>面向对象</type>
</lan>
<lan id="2">
<name>C</name>
<ide>VS</ide>
<type>面向过程</type>
</lan>
<lan id="3">
<name>C#</name>
<ide>vs</ide>
<type>面向对象</type>
</lan>
</Languages>

解析

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
48
49
50
51
52
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text = (TextView) findViewById(R.id.text);

try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(getAssets().open("xmlfile.xml"));
Element element = document.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("lan");
text.append("\n");
for (int i = 0; i < nodeList.getLength(); i++) {
Element lan = (Element) nodeList.item(i);
text.append(lan.getAttribute("id") + "\n");
text.append(lan.getElementsByTagName("name").item(0).getTextContent() + " ");
text.append(lan.getElementsByTagName("ide").item(0).getTextContent() + " ");
text.append(lan.getElementsByTagName("type").item(0).getTextContent() + " ");
text.append("\n");
}

} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

根据名称读取资源id

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.lang.reflect.Field;
import android.content.Context;

public class ResourceUtil {

public static int getLayoutId(Context sContext, String paramString) {
if (sContext == null) {
return 0;
}
return sContext.getResources().getIdentifier(paramString, "layout", sContext.getPackageName());
}

public static int getStringId(Context sContext, String paramString) {
if (sContext == null) {
return 0;
}
return sContext.getResources().getIdentifier(paramString, "string", sContext.getPackageName());
}

public static String getStringsValue(Context sContext, String key) {
int id = ResourceUtil.getStringId(sContext, key);
if (id != 0) {
return sContext.getResources().getString(id);
}
return "";
}

public static int getDrawableId(Context sContext, String paramString) {
if (sContext == null) {
return 0;
}
return sContext.getResources().getIdentifier(paramString, "drawable", sContext.getPackageName());
}

public static int getStyleId(Context sContext, String paramString) {
if (sContext == null) {
return 0;
}
return sContext.getResources().getIdentifier(paramString, "style", sContext.getPackageName());
}

public static int getId(Context sContext, String paramString) {
if (sContext == null) {
return 0;
}
return sContext.getResources().getIdentifier(paramString, "id", sContext.getPackageName());
}

public static int getColorId(Context sContext, String paramString) {
if (sContext == null) {
return 0;
}
return sContext.getResources().getIdentifier(paramString, "color", sContext.getPackageName());
}

public static int getDimenId(Context sContext, String paramString) {
if (sContext == null) {
return 0;
}
return sContext.getResources().getIdentifier(paramString, "dimen", sContext.getPackageName());
}

public static int getAnimId(Context sContext, String paramString) {
if (sContext == null) {
return 0;
}
return sContext.getResources().getIdentifier(paramString, "anim", sContext.getPackageName());
}

// 通过反射实现
public static final int[] getStyleableIntArray(Context sContext, String name) {
try {
if (sContext == null) {
return null;
}
Field field = Class.forName(sContext.getPackageName() + ".R$styleable").getDeclaredField(name);
int[] ret = (int[]) field.get(null);
return ret;
} catch (Throwable t) {
}
return null;
}

public static final int getStyleableIntArrayIndex(Context sContext, String name) {
try {
if (sContext == null) {
return 0;
}
// use reflection to access the resource class
Field field = Class.forName(sContext.getPackageName() + ".R$styleable").getDeclaredField(name);
int ret = (Integer) field.get(null);
return ret;
} catch (Throwable t) {
}
return 0;
}
}

读取所有

1
2
3
4
5
6
7
8
9
10
11
12
13
private Map<Integer, String> getStringsValue() {
Map<Integer, String> map = new HashMap<>();
Field[] fields = R.string.class.getDeclaredFields();
//Field[] fields = R.color.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
int id = getResources().getIdentifier(fields[i].getName(), "string", getPackageName());
String value = getResources().getString(id);
map.put(id, value);
}
System.out.println(map);
return map;
}