前端之家收集整理的这篇文章主要介绍了
pull方式读xml,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
url.xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<urlfile>
<url>http://192.168.1.2/wh/UploadData</url>
</urlfile>
<pre name="code" class="java">public class getXmlValue {
public static String getUrl(InputStream inputStream){
String urltext=null;
XmlPullParser xmlParser = Xml.newPullParser();
try {
xmlParser.setInput(inputStream,"utf-8");
int evtType=xmlParser.getEventType();
while(evtType!=XmlPullParser.END_DOCUMENT){
switch(evtType){
case XmlPullParser.START_TAG:
String tag = xmlParser.getName();
//如果是river标签开始,则说明需要实例化对象了
if ("url".equals(tag)) {
urltext=xmlParser.nextText();
}
break;
default:break;
}
evtType=xmlParser.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return urltext;
}
}
<pre name="code" class="java"> public static void save(String URL,OutputStream out) throws Exception{
XmlSerializer serializer= Xml.newSerializer();
serializer.setOutput(out,"UTF-8");
serializer.startDocument("UTF-8",true);
serializer.startTag(null,"urlfile");
serializer.startTag(null,"url");
serializer.text(URL);
serializer.endTag(null,"url");
serializer.endTag(null,"urlfile");
serializer.endDocument();
out.flush();
out.close();
}
InputStream inputStream= this.getClass().getClassLoader().getResourceAsStream("url.xml");
processURL=getXmlValue.getUrl(inputStream);
if(processURL.length()==0){
<span style="white-space:pre"> </span>Toast.makeText(this,"url.xml文件有误",Toast.LENGTH_LONG).show();
}