android 怎么读取一个xml
1个回答
2016-07-04 · 百度知道合伙人官方认证企业
兄弟连教育
兄弟连教育成立于2006年,11年来专注IT职业教育,是国内专业的IT技术培训学校。2016年成功挂牌新三板(股票代码:839467)市值过亿。开设专注程序员培训专注php、Java、UI、云计算、Python、HTML5、
向TA提问
关注
展开全部
关于具体使用方法,举例如下:
1、updatelog.xml文件位于res/xml/文件夹下:
<?xml version="1.0" encoding="utf-8"?>
<updatelog>
<release
version="1.0"
versioncode="1.1">
<log>1、这个文件在res/xml文件夹下;</log>
<log>2、这个文件的名字是“updatelog.xml”</log>
<log>3、这是一个xml文件</log>
</release>
</updatelog>
2、在代码中调用方法如下:
public class ResourcesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.resources_layout );
mTextView = ( TextView )findViewById( R.id.textId );
getFileResources( );
}
private void getFileResources( ){
String packageName = getPackageName( );
try {
Resources resources = this.getPackageManager( ).getResourcesForApplication( packageName );
int resId = resources.getIdentifier( "updatelog", "xml", packageName );
String log = getUpdatelog( resId, resources );
int appId = resources.getIdentifier( "action_settings", "string", packageName );
String appName = resources.getString( appId );
mTextView.setText( appName + "\n" + log );
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
private String parseTag( XmlResourceParser xml ) throws XmlPullParserException, IOException {
String result = "Version: "+ xml.getAttributeValue(null, "version") + "\n" + "VersionCode: " + xml.getAttributeValue(null, "versioncode") + "\n";
int eventType = xml.getEventType( );
while( ( eventType != XmlPullParser.END_TAG ) || ( xml.getName( ).equals( "log" ) ) ) {
if( (eventType == XmlPullParser.START_TAG) && ( xml.getName( ).equals( "log" ) ) ) {
eventType = xml.next( );
result = result + xml.getText( ) + "\n";
}
eventType = xml.next( );
}
return result;
}
private String getUpdatelog( int resourceId, Resources resource ) {
String result = "";
XmlResourceParser xmlResParser = resource.getXml( resourceId );
try {
int eventType = xmlResParser.getEventType( );
while ( eventType != XmlPullParser.END_DOCUMENT ) {
if ( ( eventType == XmlPullParser.START_TAG ) && ( xmlResParser.getName( ).equals( "release" ) ) ) {
result = result + parseTag( xmlResParser );
}
eventType = xmlResParser.next( );
}
} catch (XmlPullParserException e) {
System.out.println( e.getMessage( ) );
} catch (IOException e) {
System.out.println( e.getMessage( ) );
} finally {
xmlResParser.close( );
}
return result;
}
private TextView mTextView = null;
}
1、updatelog.xml文件位于res/xml/文件夹下:
<?xml version="1.0" encoding="utf-8"?>
<updatelog>
<release
version="1.0"
versioncode="1.1">
<log>1、这个文件在res/xml文件夹下;</log>
<log>2、这个文件的名字是“updatelog.xml”</log>
<log>3、这是一个xml文件</log>
</release>
</updatelog>
2、在代码中调用方法如下:
public class ResourcesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.resources_layout );
mTextView = ( TextView )findViewById( R.id.textId );
getFileResources( );
}
private void getFileResources( ){
String packageName = getPackageName( );
try {
Resources resources = this.getPackageManager( ).getResourcesForApplication( packageName );
int resId = resources.getIdentifier( "updatelog", "xml", packageName );
String log = getUpdatelog( resId, resources );
int appId = resources.getIdentifier( "action_settings", "string", packageName );
String appName = resources.getString( appId );
mTextView.setText( appName + "\n" + log );
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
private String parseTag( XmlResourceParser xml ) throws XmlPullParserException, IOException {
String result = "Version: "+ xml.getAttributeValue(null, "version") + "\n" + "VersionCode: " + xml.getAttributeValue(null, "versioncode") + "\n";
int eventType = xml.getEventType( );
while( ( eventType != XmlPullParser.END_TAG ) || ( xml.getName( ).equals( "log" ) ) ) {
if( (eventType == XmlPullParser.START_TAG) && ( xml.getName( ).equals( "log" ) ) ) {
eventType = xml.next( );
result = result + xml.getText( ) + "\n";
}
eventType = xml.next( );
}
return result;
}
private String getUpdatelog( int resourceId, Resources resource ) {
String result = "";
XmlResourceParser xmlResParser = resource.getXml( resourceId );
try {
int eventType = xmlResParser.getEventType( );
while ( eventType != XmlPullParser.END_DOCUMENT ) {
if ( ( eventType == XmlPullParser.START_TAG ) && ( xmlResParser.getName( ).equals( "release" ) ) ) {
result = result + parseTag( xmlResParser );
}
eventType = xmlResParser.next( );
}
} catch (XmlPullParserException e) {
System.out.println( e.getMessage( ) );
} catch (IOException e) {
System.out.println( e.getMessage( ) );
} finally {
xmlResParser.close( );
}
return result;
}
private TextView mTextView = null;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询