Sarah Sarah - 5 months ago 23
Java Question

load values of ArrayList file into a ArrayList

I'm trying to load the data of ArrayList file into ArrayList, but i got this exception error:

07-07 16:52:21.741: W/System.err(1941): org.xml.sax.SAXParseException: Unexpected token (position:TEXT [Ljava.lang.Stri...@1:28 in java.io.StringReader@f7179f5)
07-07 16:52:21.741: W/System.err(1941): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146)
07-07 16:52:21.743: W/System.err(1941): at com.example.ActivityQuizScreen.addflags(ActivityQuizScreen.java:253)
07-07 16:52:21.743: W/System.err(1941): at com.example.ActivityQuizScreen.onCreate(ActivityQuizScreen.java:108)
07-07 16:52:21.743: W/System.err(1941): at android.app.Activity.performCreate(Activity.java:6237)
07-07 16:52:21.743: W/System.err(1941): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
07-07 16:52:21.745: W/System.err(1941): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
07-07 16:52:21.745: W/System.err(1941): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
07-07 16:52:21.745: W/System.err(1941): at android.app.ActivityThread.-wrap11(ActivityThread.java)
07-07 16:52:21.745: W/System.err(1941): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
07-07 16:52:21.746: W/System.err(1941): at android.os.Handler.dispatchMessage(Handler.java:102)
07-07 16:52:21.746: W/System.err(1941): at android.os.Looper.loop(Looper.java:148)
07-07 16:52:21.746: W/System.err(1941): at android.app.ActivityThread.main(ActivityThread.java:5417)
07-07 16:52:21.747: W/System.err(1941): at java.lang.reflect.Method.invoke(Native Method)
07-07 16:52:21.747: W/System.err(1941): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
07-07 16:52:21.747: W/System.err(1941): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-07 16:52:21.877: W/EGL_emulation(1941): eglSurfaceAttrib not implemented
07-07 16:52:21.877: W/OpenGLRenderer(1941): Failed to set EGL_SWAP_BEHAVIOR on surface 0xac0b28c0, error=EGL_SUCCESS
07-07 16:52:22.429: E/Surface(1941): getSlotFromBufferLocked: unknown buffer: 0xabff43b0
07-07 16:53:21.877: W/EGL_emulation(1941): eglSurfaceAttrib not implemented
07-07 16:53:21.880: W/OpenGLRenderer(1941): Failed to set EGL_SWAP_BEHAVIOR on surface 0xac0b2ee0, error=EGL_SUCCESS


the code is :

@SuppressWarnings("unchecked")
private void addflags() throws ParserConfigurationException, SAXException, IOException{

ArrayList<Flag> flagList = new ArrayList<Flag>();

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource source = new InputSource();
source.setCharacterStream(new StringReader((getResources().getStringArray(R.array.flags)).toString()));
org.w3c.dom.Document doc = db.parse(source); // ActivityQuizScreen.java:253 is about here
NodeList nodes = doc.getElementsByTagName("item");
for(int i = 0; i< nodes.getLength();i++)
{
NamedNodeMap attr = nodes.item(i).getAttributes();

String name = attr.getNamedItem("name").getNodeValue();
String CountryCode = attr.getNamedItem("pic").getNodeValue().toLowerCase();
int pic = getResources().getIdentifier(CountryCode, "drawable", getPackageName());
Flag flag = new Flag(
attr.getNamedItem("name").getNodeValue(),
getResources().getIdentifier(CountryCode, "drawable", getPackageName()));
flagList.add(flag);

}


Array list file is like:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="flags">
<item pic="AF" name="Afghanistan" />
<item pic="AL" name="Albania" />
<item pic="DZ" name="Algeria" />
<item pic="AS" name="American Samoa" />
<item pic="AD" name="Andorra" />
: : :
<item pic="YE" name="Yemen" />
<item pic="ZM" name="Zambia" />
<item pic="ZW" name="Zimbabwe" />
<item pic="AX" name="Åland Islands" />

</string-array>
</resources>


How i can solve this, or is there better solutions to load arrayl list from a file?? Anyone plzzzzzz?

Thank you!!

Answer

1) You need to create an assets folder: Where to place Assets folder in Android Studio

2) Add this xml file (Example name: flag_array.xml) in your assets folder:

resources>

<string-array name="flags">
    <item pic="AF"  name="Afghanistan"  />
    <item pic="AL"  name="Albania"  />
    <item pic="DZ"  name="Algeria"  />
    <item pic="AS"  name="American Samoa"  />
    <item pic="AD"  name="Andorra"  />
    <item pic="YE"  name="Yemen"  />
    <item pic="ZM"  name="Zambia"  />
    <item pic="ZW"  name="Zimbabwe"  />
    <item pic="AX"  name="Åland Islands"  />
</string-array>

/resources>

3) Replace your code in the addflags() method with this:

    ArrayList<Flag> flagList = new ArrayList<Flag>();
    DocumentBuilder db = null;
    try {

        db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        AssetManager assetManager = getAssets();
        InputStream is = assetManager.open("flag_array.xml");
        org.w3c.dom.Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("item");
        for(int i = 0; i< nodes.getLength();i++) {
            NamedNodeMap attr = nodes.item(i).getAttributes();

            String name = attr.getNamedItem("name").getNodeValue();
            String CountryCode = attr.getNamedItem("pic").getNodeValue().toLowerCase();

            int pic = getResources().getIdentifier(CountryCode, "drawable", getPackageName());
            Flag flag = new Flag(
                    attr.getNamedItem("name").getNodeValue(),
                    getResources().getIdentifier(CountryCode, "drawable", getPackageName()));
            flagList.add(flag);
        }

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }