android中读XML文件
16lz
2022-07-05
res/xml中的XML文件会被Android平台(如果使用Eclipse)自动提取并编译为资源。
在程序中可能通过Android支持的XML格式来读取它。 people.xml文件
<people>
<personfirstname="hu"lastname="bing"/>
<personfirstname="hu"lastname="dashi"/>
<personfirstname="zheng"lastname="wei"/>
</people> 代码片段
void readXmlFile()
{
XmlPullParserparser=this.getResources().getXml(R.xml.people);
Stringname=null;
Stringfirst=null;
Stringlast=null;
StringattrName=null;
StringattrValue=null;
try {
while(parser.next()!=XmlPullParser.END_DOCUMENT)
{
name=parser.getName();
first=null;
last=null;
if( (name!=null)&&name.equals("person"))
{
intsize=parser.getAttributeCount();
for (inti=0;i<size;i++)
{
attrName=parser.getAttributeName(i);
attrValue=parser.getAttributeValue(i);
if (attrName!=null)
{
if ( attrName.equals("firstname") )
{
first=attrValue;
}
elseif ( attrName.equals("lastname") )
{
last=attrValue;
}
}
}
if (first!=null&&last!=null)
Log.i(tag,first+last);
}
}
} catch (XmlPullParserExceptione)
{
Log.i(tag,"Xmlerro",e);
}
catch (IOExceptione)
{
Log.i(tag,"IOerro",e);
}
}
在程序中可能通过Android支持的XML格式来读取它。 people.xml文件
<people>
<personfirstname="hu"lastname="bing"/>
<personfirstname="hu"lastname="dashi"/>
<personfirstname="zheng"lastname="wei"/>
</people> 代码片段
void readXmlFile()
{
XmlPullParserparser=this.getResources().getXml(R.xml.people);
Stringname=null;
Stringfirst=null;
Stringlast=null;
StringattrName=null;
StringattrValue=null;
try {
while(parser.next()!=XmlPullParser.END_DOCUMENT)
{
name=parser.getName();
first=null;
last=null;
if( (name!=null)&&name.equals("person"))
{
intsize=parser.getAttributeCount();
for (inti=0;i<size;i++)
{
attrName=parser.getAttributeName(i);
attrValue=parser.getAttributeValue(i);
if (attrName!=null)
{
if ( attrName.equals("firstname") )
{
first=attrValue;
}
elseif ( attrName.equals("lastname") )
{
last=attrValue;
}
}
}
if (first!=null&&last!=null)
Log.i(tag,first+last);
}
}
} catch (XmlPullParserExceptione)
{
Log.i(tag,"Xmlerro",e);
}
catch (IOExceptione)
{
Log.i(tag,"IOerro",e);
}
}
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- 读取android手机流量信息
- android 使用html5作布局文件: webview跟javascript交互
- Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
- android“设置”里的版本号
- Android开发环境搭建
- Android(安卓)Resource介绍和使用
- 2014.01.21 ——— android 关联android-support源码