xml中配置数据代码中使用
1.res/xml/目录下创建config_formal.xml文件
<config>
<appType>02</appType>
<customer>jt</customer>
<systemID>000</systemID>
<key>msd12345</key>
<blueWeight>false</blueWeight>
<autoUpload>true</autoUpload>
<debug>true</debug>
<project>Business_web</project>
<addrs>
<id>203</id>
<id>162</id>
<id>54</id>
<id>41</id>
</addrs>
<port>22220</port>
<suffix></suffix>
</config>
2.代码中读取(xml文件的pull解析)
public void parser() throws Exception{
XmlResourceParser xmlParser = null;
xmlParser = context.getResources().getXml(R.xml.config_formal);
int event = xmlParser.getEventType(); // 获取解析状态,返回的是int型数字状态
while (event != XmlPullParser.END_DOCUMENT) { // 如果状态不是结束事件END_DOCUMENT,就递归
switch (event) {
case XmlPullParser.START_DOCUMENT:{ // 如果为开始解析头标签START_DOCUMENT,初始化数据
break;
}
case XmlPullParser.START_TAG:{ //如果为开始解析属性START_TAG,则获取数据
if("appType".equals(xmlParser.getName())){
this.appType = xmlParser.nextText();
}else if("customer".equals(xmlParser.getName())){
this.customer = xmlParser.nextText();
}else if("key".equals(xmlParser.getName())){
this.key = xmlParser.nextText();
}else if("systemID".equals(xmlParser.getName())){
this.systemID = xmlParser.nextText();
}else if("autoUpload".equals(xmlParser.getName())){
this.autoUpload = Boolean.parseBoolean(xmlParser.nextText());
}else if("debug".equals(xmlParser.getName())){
this.debug = Boolean.parseBoolean(xmlParser.nextText());
}else if("project".equals(xmlParser.getName())){
this.project = xmlParser.nextText();
}else if("addrs".equals(xmlParser.getName())){
addrs = new ArrayList<String>();
}else if("id".equals(xmlParser.getName())){
addrs.add(xmlParser.nextText());
}else if("port".equals(xmlParser.getName())){
port = xmlParser.nextText();
}else if("suffix".equals(xmlParser.getName())){
suffix = xmlParser.nextText();
}
break;
}
case XmlPullParser.END_TAG:{
break;
}
}
event = xmlParser.next();
}
}
3.组装url
public String getURL(){
des = new DES(key);
StringBuffer sb = new StringBuffer();
sb.append("http://");
for(int i=0; i<addrs.size(); i++){
sb.append(decode(addrs.get(i)));
sb.append(".");
}
sb.append(decode(suffix));
String tmp = sb.toString();
if(tmp.endsWith(".")){
sb.setLength(0);
sb.append(tmp.substring(0, tmp.length()-1));
}
sb.append(":");
sb.append(decode(port));
sb.append("/");
sb.append(decode(project));
return sb.toString();
}