How to write value on XML file in Android?

Tutorials with advanced 'difficulty' and more Lines of Code.

How to write value on XML file in Android?

Postby Virtue » Fri Nov 28, 2008 10:14 am

Hi all,
I want to write some values on XML file to save application configuration.
I have to use method?

Thanks in advance.
Virtue. :)
Virtue
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 28, 2008 9:33 am

Top

How to Create XML File through Android Program?

Postby prajkti.khadse » Tue Jul 14, 2009 7:27 am

Hi Virtue
Me too want to Create/Write XML file to store My Application Settings.
Did you get any Solution for?

Regards,
Prajkti
prajkti.khadse
Developer
Developer
 
Posts: 32
Joined: Thu Mar 19, 2009 1:01 pm
Location: Bangalore

Postby Shone » Wed Jul 15, 2009 3:49 pm

There is a package javax.xml.parsers in the SDK which supports XML parsing and writing.
Shone
Freshman
Freshman
 
Posts: 3
Joined: Tue Jul 14, 2009 10:59 am

xml parsing from folder

Postby myster23 » Thu Jul 16, 2009 10:33 am

Can a xml file be parsed from a folder from an Android project? if yes which folder is that?
I want to parse the following tags from a folder:
<Data>
<Question1>Do you like C++</Question1>
<Option1>Yes</Option1>
<Option2>No</Option2>
</Data>
myster23
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Apr 23, 2009 7:53 am

Postby Shone » Thu Jul 16, 2009 11:06 am

You can put it into the /res folder in your Android project.
@see http://developer.android.com/guide/topi ... -i18n.html
Shone
Freshman
Freshman
 
Posts: 3
Joined: Tue Jul 14, 2009 10:59 am

Postby TheSmile » Sun Jul 19, 2009 11:57 pm

Hi Virtue,

according to this link here http://developer.android.com/guide/topi ... orage.html you can use SharedPreferences to store app configuration. It's an easy way to do it. It's a map with key - value pairs that will be created.
Whatever comes ... just keep on smiling :)

http://blog.thesmile.de
User avatar
TheSmile
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jul 17, 2009 2:22 pm
Location: Germany

Top

convert

Postby myster23 » Wed Jul 22, 2009 8:11 am

The question is this:
How can I convert an InputStream instrem to InputSource ;

For example, is the following allowed ?

InputSource in = new InputSource();
InputStream instrem;
instrem=Resources.getSystem().openRawResource(R.raw.questions);
in=instrem;

or

How can I convert an InputSource instrem to File ;

For example, is the following allowed ?

File rd;
InputStream instrem;
instrem=Resources.getSystem().openRawResource(R.raw.questions);
rd=instrem;

What can I do so that this can be allowed ?
myster23
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Apr 23, 2009 7:53 am

Postby TheSmile » Wed Jul 22, 2009 9:17 am

Hi myster23.

looks like you have more of a java problam rather then an android problem.
I don't know if this line of code

Code: Select all
instrem=Resources.getSystem().openRawResource(R.raw.questions);


is correct, but if it is, you can just create a new InputSource and pass the InputStream as a parameter into the constructor.
Have a look at the java docs here:
http://java.sun.com/j2se/1.4.2/docs/api ... ource.html
Whatever comes ... just keep on smiling :)

http://blog.thesmile.de
User avatar
TheSmile
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jul 17, 2009 2:22 pm
Location: Germany

Postby myster23 » Tue Aug 04, 2009 7:01 am

The following program shows: error: Resource ID #0x7f040000
What is the problem? How can I fix it?

ParseRes.java--->

Code: Select all
try {
TextView tx1=new TextView(this);
TextView tvx=new TextView(this);
        XmlResourceParser xpp=Resources.getSystem().getXml(R.xml.myxml);
       
        int eventType=xpp.getEventType();
          eventType=xpp.nextTag();
       while(eventType!=XmlResourceParser.END_DOCUMENT)
      {    
         if(xpp.getName().equals("name"))
         {   
            tx1.setText("YES!");
         }
         else { tx1.setText("NO!");  }
          
        eventType=xpp.nextTag();
      }
     this.setContentView(tx1);
   }catch (Exception e) {
      // TODO Auto-generated catch block
   tvx.setText("Error: " + e.getMessage( ));
        Log.e(MY_DEBUG_TAG, "WeatherQueryError", e);
      this.setContentView(tvx);
   }



R.java--->

package com.android.Parsing;

Code: Select all
public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f050001;
        public static final int hello=0x7f050000;
    }
    public static final class xml {
        public static final int myxml=0x7f040000;
    }
}


The xml file I parse is this:
myxml.xml--->

Code: Select all
<?xml version="1.0" encoding="utf-8" ?>
<Person>
<name>Bill</name>
<last>Gates</last>
</Person>
myster23
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Apr 23, 2009 7:53 am

Postby TheSmile » Tue Aug 04, 2009 9:44 am

Hi,

I haven't testet your code but just having a look at your post here you changed your code from your last post.

You wrote:
Code: Select all
XmlResourceParser xpp=Resources.getSystem().getXml(R.xml.myxml);


You need to load raw data (your xml) with this method:
Code: Select all
openRawResource(int id) from the Resource class 


Second, if you would read the docs (sorry to says that but is's obvious) you cannot use
Code: Select all
Resources.getSystem()
for your application because it returns the system resources and NOT the application resources.

You need to have a look at the API-docs for the Resource class here.
Whatever comes ... just keep on smiling :)

http://blog.thesmile.de
User avatar
TheSmile
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jul 17, 2009 2:22 pm
Location: Germany

Postby myster23 » Wed Aug 05, 2009 7:05 am

Firstly, I changed the XmlResourceParser xpp=Resources.getSystem().getXml(R.xml.myxml)
to XmlResourceParser xpp=getResources().getXml(R.xml.myxml) but it shows the
Error: Binary XML file line#-1:expected start or end tag (position:Binary XML file line #-1)

What does it mean ?

Secondly, the getResources().openRawResources(R.raw.myxml) returns an InputStream that is,
InputStream inp=getResources().openRawResource(R.raw.myxml). How can I read the inp ?
myster23
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Apr 23, 2009 7:53 am

xml parsing

Postby hPushpa18 » Fri Nov 06, 2009 6:07 am

Hi,

Are you able to extract the data from xml now?If yes, can u plz attach your project(entire source code) and send it to me.I also need to do xmlparsing.Its really urgetn.Plz help

Thanks and Regards,
Pushpa
hPushpa18
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 02, 2009 11:08 am
Location: Bangalore

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests