1

I am creating a simple app in which I have an xml file with about 70,000 lines. The xml file looks like this:

<!--<?xml version="1.0" encoding="utf-8"?>-->
<!--<resources></resources>-->
<Root>
  <Item ItemNumber="OXB433"/>
  <Item ItemNumber="JMQ14P"/>
  <Item ItemNumber="A882E2"/>
  <Item ItemNumber="LLMH3K"/>
  <Item ItemNumber="BEM3QD"/>
  <Item ItemNumber="XKMM6R"/>
  *** about 70,000 of these lines ***
</Root>

And this is how I am trying to parse the xml and store it in an ArrayList of strings.

  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      try {
          String item = getItemFromXML(this);
      } catch (XmlPullParserException e) {
      } catch (IOException e) {
      }
      final String[] items = item.split("\n");
  }

  public String getItemFromXML(Activity activity) throws XmlPullParserException, IOException{
    StringBuffer stringBuffer = new StringBuffer();
    Resources res = activity.getResources();
    XmlResourceParser xpp = res.getXml(R.xml.items);
    xpp.next();
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT){
        if (eventType == XmlPullParser.START_TAG){
            if (xpp.getName().equals("Item")){
                stringBuffer.append(xpp.getAttributeValue(null, "ItemNumber") + "\n");
            }
        }
        eventType = xpp.next();
    }
    return stringBuffer.toString();
}

Now here is the problem, when I click "run", gradle build takes about 3 minutes to build and after troubleshooting and debugging, I found out that it takes so long because of 70,000 lines in my xml file. When I reduced my xml file to 10 lines, gradle build finished in 8 seconds. So how do I solve this problem? I can't reduce the xml file and I can't wait 3 minutes for gradle to build.

Note: my file "items.xml" is inside an folder named "xml" which is under "res" folder.

Thank you :)

Parth Bhoiwala
  • 1,282
  • 3
  • 19
  • 44
  • Put it inside `assets` folder instead. Sure you will lose check that you're xml file is valid – Eugen Martynov Jun 03 '16 at 14:52
  • I moved the `items.xml` into `assets` folder but now it says `Element Root must be declared` and `Cannot resolve symbol Item`. How do I fix that? – Parth Bhoiwala Jun 03 '16 at 15:04
  • You mean that you have now compilation errors? – Eugen Martynov Jun 03 '16 at 15:05
  • Ya, the problem is with this line of code in main activity `XmlResourceParser xpp = res.getXml(R.xml.items);` What do I modify this line to? – Parth Bhoiwala Jun 03 '16 at 15:07
  • This is our tradeoff. If you don't want to parse xml during compile time then you have to parse during runtime http://stackoverflow.com/questions/5557885/android-how-to-load-xml-file-from-assets-directory – Eugen Martynov Jun 03 '16 at 15:08
  • You also might create flavour develop and put in develop folder small xml, and in production your huge one – Eugen Martynov Jun 03 '16 at 15:09
  • It's almost working, I just need to modify my `getItemsFromXml()` function because the xml is now in assets instead of res folder. But I don't know which piece of code to change. How to modify the function to make it get xml from assets and parse it? – Parth Bhoiwala Jun 03 '16 at 15:34

0 Answers0