res/xml中的XML文件会被Android平台(如果使用Eclipse)自动提取并编译为资源。
在程序中可能通过Android支持的XML格式来读取它。
在程序中可能通过Android支持的XML格式来读取它。
people.xml文件
<people>
<personfirstname="hu"lastname="bing"/>
<personfirstname="hu"lastname="dashi"/>
<personfirstname="zheng"lastname="wei"/>
</people>
<people>
<personfirstname="hu"lastname="bing"/>
<personfirstname="hu"lastname="dashi"/>
<personfirstname="zheng"lastname="wei"/>
</people>
代码片段
voidreadXmlFile()
{
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);
}
}
voidreadXmlFile()
{
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);
}
}