xml中配置数据代码中使用

1.res/xml/目录下创建config_formal.xml文件

02
jt
000
msd12345
false
true
true
Business_web

203
162
54
41

22220


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();
                
                }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 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();
}

你可能感兴趣的:(xml中配置数据代码中使用)