简体   繁体   English

使用DOM解析器读取和写入内部android xml文件

[英]Reading and writing to internal android xml file using DOM parser

I am new to Android I am trying to read and write some information from and to xml file in Android. 我是Android的新手,我试图在Android的xml文件中读写信息。 I thought it would be possible if I use DOM parser to do such stuff... I am reading file at the beggining of an application to get the data and trying to save before the application terminates to store configuration changes. 我以为如果使用DOM解析器来做这些事情是可能的...我在应用程序的开头读取文件以获取数据,并试图在应用程序终止以存储配置更改之前进行保存。 I figured out how to read file from raw/xml but it seems to be a real pain (or even impossibility) to save modifyied xml back here... Maybe I am taking wrong approach, but I believe it should be possible to somehow modify some internal xml file in Android. 我想出了如何从raw / xml读取文件,但是将修改后的xml保存在这里似乎是一件很痛苦的事(甚至是不可能)...也许我采用了错误的方法,但是我认为应该可以以某种方式进行修改Android中的一些内部xml文件。 How to go about it? 怎么做呢?

You can not modify or update raw/xml file directory. 您不能修改或更新raw/xml文件目录。 Android /res directory is Read-Only . Android /res目录为只读 So you can only read file from it not write back allowed. 因此,您只能从中读取文件,不允许回写。 ( As Android .apk file has Read-Only permission) . 因为Android .apk文件具有只读权限)

cant modify存储在android app的xml folder中的xml folder 。在将设备上安装应用程序到sdcard/internal storage时需要copy该文件,然后进行修改。在这种情况下,您可以使用Dom Parser从sdcard /内部读取和写入xml文件设备的存储。

@user370305 is right!! @ user370305是正确的!

simply logic is that you cannot create a file at runtime internally in the storage (raw,asset,layout etc.) So you can create a different file externally by giving the permission "write_external_storage". 简单的逻辑是,您无法在运行时在存储内部(原始,资产,布局等)创建文件。因此,可以通过授予权限“ write_external_storage”在外部创建其他文件。 Then you can read or write. 然后,您可以读取或写入。

Here is an example 这是一个例子

   public void readXML() throws IOException{ 

         //get the xml file from the raw folder
        InputStream is = getResources().openRawResource(
                    R.raw. config );

        Resources r = getResources();
        AssetManager assetManager = r .getAssets();

         //then write the dummy file
        File f = new File(Environment.getExternalStorageDirectory(), "dummy.xml" );
        OutputStream os = new FileOutputStream(f , true);



         final int buffer_size = 1024 * 1024;
         try
        {
            byte [] bytes = new byte [buffer_size ];
            for (;;)
            {
                int count = is.read( bytes, 0, buffer_size );
                if (count == -1)
                    break ;
                os.write( bytes , 0, count );
            }
            is.close();
            os.close();
        }
         catch (Exception ex )
        {
            ex.printStackTrace();
        }


         //then parse it
                    parseConfigXML( f);

  }

   public String parseConfigXML(File configXML ) {

        XmlPullParser xpp = null ;
         String httpUrl= "";
        FileInputStream fis ;
        XmlPullParserFactory factory = null ;
         try {

               fis = new FileInputStream( configXML);
               factory = XmlPullParserFactory.newInstance();
               factory .setNamespaceAware( true);
               xpp = factory .newPullParser();
               xpp .setInput( new InputStreamReader( fis));

               int eventType = xpp.getEventType();
               while (eventType != XmlPullParser. END_DOCUMENT) {
                     if (eventType == XmlPullParser. START_DOCUMENT) {

                    } else if (eventType == XmlPullParser. START_TAG) {

                    } else if (eventType == XmlPullParser. END_TAG) {

                    } else if (eventType == XmlPullParser. TEXT) {

                           httpUrl += xpp .getText().replace( "\n", "" );

                    }
                     eventType = xpp .next();
              }
        } catch (FileNotFoundException e1 ) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
        } catch (XmlPullParserException e ) {
               // TODO Auto-generated catch block
               e.printStackTrace();
        } catch (IOException e ) {
               // TODO Auto-generated catch block
               e.printStackTrace();
        }

         return httpUrl .trim().replace( "\n", "" );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM