android读写xml文件,能读不能写入

android读写xml文件,能读不能写入,第1张

我汗,LZ,你只是把数据适配到了serializer里面,最后你要写进outputstream里面才行啊,flush的意思是将缓存写进硬盘里,但是你的缓存根本就是空的,你要调用outputstream.write将serializer的东西写进去才行。

关于具体使用方法,举例如下:

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

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11452458.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存