1

While doing this task I followed XML Parsing tutorial from http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/ Very good tutorial. One minus for me it is taking XML from URL. I need to take from R.raw folder. I searched for solutions, found many solutions with InputStream(eg: Load local xml data in listview in android) I have put one line of code

InputStream is = res.openRawResource(R.raw.localxmlfileName);

But it is underlining res and giving mistake:[res cannot be resolved]. How to solve this problem and am I going right way?

Community
  • 1
  • 1
Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109

2 Answers2

8

For XML, a generally better option is to put the file in res/xml/, then get an XmlPullParser for that XML by calling getResources().getXml(). The XmlPullParser allows you to work your way through the XML events (new document, new element, etc.) and read in what you need.

For example, given res/xml/words.xml like this:

<words>
  <word value="lorem" />
  <word value="ipsum" />
  <word value="dolor" />
  <word value="sit" />
  <word value="amet" />
  <word value="consectetuer" />
  <word value="adipiscing" />
  <word value="elit" />
  <word value="morbi" />
  <word value="vel" />
  <word value="ligula" />
  <word value="vitae" />
  <word value="arcu" />
  <word value="aliquet" />
  <word value="mollis" />
  <word value="etiam" />
  <word value="vel" />
  <word value="erat" />
  <word value="placerat" />
  <word value="ante" />
  <word value="porttitor" />
  <word value="sodales" />
  <word value="pellentesque" />
  <word value="augue" />
  <word value="purus" />
</words>

you would read them into an ArrayList<String> like this (from inside an activity, for example):

  ArrayList<String> items=new ArrayList<String>();
  XmlPullParser xpp=getResources().getXml(R.xml.words);

  while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) {
    if (xpp.getEventType()==XmlPullParser.START_TAG) {
      if (xpp.getName().equals("word")) {
        items.add(xpp.getAttributeValue(0));
      }
    }

    xpp.next();
  }

(there's a try/catch block in there too, but I trimmed that out for the sake of simplicity)

The advantage is that XML stored in a res/xml/ can be read in this way about ten times faster than using an XML parser on an InputStream from res/raw/, or from a plain file. That's because Google seriously optimized the act of reading in XML from resource directories known to hold XML (res/xml/, res/layout/, etc.), partly by pre-compiling the XML in to a "binary XML" format as part of the build process.

The disadvantage is that fewer developers are familiar with XmlPullParser.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    We always get something new to learn from you. Thanks sir for enlightening us new developers. – Shobhit Puri Jul 13 '13 at 22:33
  • Thank you for detailed answer with examples. How to implement ArrayList<> items into ListView? – Joe Rakhimov Jul 13 '13 at 22:45
  • @JoeRichard: Use an `ArrayAdapter<>`. – CommonsWare Jul 13 '13 at 22:48
  • OK. What if listview includes more than one textview. I mean one list item has 3 characteristics:name, description, and price. What should I write if I will use ArrayList> items = new ArrayList>(); //instead of// ArrayList words=new ArrayList(); //because it is showing error while adding item into items [items.add(xpp.getAttributeValue(0)); //underlining add]. please help – Joe Rakhimov Jul 13 '13 at 23:00
  • Sorry for hard to understand question. I meant that how to implement items.add(xpp.getAttributeValue(0)); if one list view item has more than one text views? – Joe Rakhimov Jul 13 '13 at 23:06
  • @JoeRichard: This is getting *way* beyond comments on the answer. I would recommend that you create some sort of model object that holds your strings, then create an `ArrayAdapter`, where you can override `getView()` to populate multiple widgets in your row. If you have further questions on setting up an `ArrayAdapter`, please start another StackOverflow question, after having searched for existing materials first. – CommonsWare Jul 13 '13 at 23:07
1

res seems to be a resource variable. You need to get the resources first. Try using something like:

InputStream is = getResources().openRawResource(R.raw.localxmlfileName);
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Now it is showing openRawResource as error(underlining:undefined for the type of object). No idea what to do – Joe Rakhimov Jul 13 '13 at 22:12
  • InputStream is = getResources().openRawResource(R.raw.pizza); DocumentBuilder db = dbf.newDocumentBuilder(); //InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml)); doc = db.parse(is); – Joe Rakhimov Jul 13 '13 at 22:13
  • You might need a context for that. So try `this.getResources().openRawResource(R.raw.localxmlfileName);` if you are doing it inside a class which extends `Activity` else you need to get the `Contect` somehow. – Shobhit Puri Jul 13 '13 at 22:27
  • class extends Activity and i have put this. before getResources() it still showing error. what to do? – Joe Rakhimov Jul 13 '13 at 22:31