1

I want to write a string to a key in an xml file and then recall it later. How would I do this?

For instance, if the user were to enter a string of characters into a EditText view, I want to be able to store it and compare it against something, like a stored string.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Jeeter
  • 5,887
  • 6
  • 44
  • 67
  • 2
    Why do you want to store the string in an XML file? Could you store it in an Object or a local database? How long do you need to hold onto this information? – l15a Jun 28 '12 at 18:21

2 Answers2

1

In Android, the XmlPullParser is recommended when doing XML. Here's a very simple example on how to use it.

 import java.io.IOException;
 import java.io.StringReader;

 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException.html;
 import org.xmlpull.v1.XmlPullParserFactory;

 public class SimpleXmlPullApp
 {

     public static void main (String args[])
         throws XmlPullParserException, IOException
     {
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput(new StringReader ("<foo>Hello World!</foo>"));
         int eventType = xpp.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
          if(eventType == XmlPullParser.START_DOCUMENT) {
              System.out.println("Start document");
          } else if(eventType == XmlPullParser.END_DOCUMENT) {
              System.out.println("End document");
          } else if(eventType == XmlPullParser.START_TAG) {
              System.out.println("Start tag "+xpp.getName());
          } else if(eventType == XmlPullParser.END_TAG) {
              System.out.println("End tag "+xpp.getName());
          } else if(eventType == XmlPullParser.TEXT) {
              System.out.println("Text "+xpp.getText());
          }
          eventType = xpp.next();
         }
     }
 }

Here are some links that might help:

Dave
  • 4,050
  • 6
  • 30
  • 35
  • how do I specify which document I am pulling from? – Jeeter Jul 04 '12 at 00:28
  • 1
    Use the setInput method to specify an InputStream. Wrap up a File in a FileInputStream. You may want to consider looking at a simple database for this, though. Depending on what the key string is used for, even using a Preferences class might work. – Dave Jul 12 '12 at 18:51
  • Thanks! could you possibly direct me to some good database creation tutorials? – Jeeter Jul 12 '12 at 22:21
  • Have a look at this one from vogella.com: http://www.vogella.com/articles/AndroidSQLite/article.html – Dave Jul 16 '12 at 21:00
0

This blog shows an example of exactly what you are looking for. The other 30 days are also very helpful.

http://chrisrisner.com/31-Days-of-Android--Day-4%E2%80%93Our-First-App

<EditText
    android:id="@+id/editText1" />

Then in the .java file...

EditText editText1 = (EditText) findViewById( R.id.editText1 );
String textEntered = editText1.getText();

I cannot think of a reason for storing a String in an XML file other than setting a View's Text to that string, which can be done in the .java file by calling .setText( enteredText )

You should look to the blog for a better documentation though...

JuiCe
  • 4,132
  • 16
  • 68
  • 119