Parsing large XML files on Android

I am trying to parse a rather large XML file - 1 MB +, but I am having some difficulties. At first I tried to add the xml file to res / xml and parse it using XmlResourceParser, but I got an exception: "Data exceeds UNCOMPRESS_DATA_MAX". After a little research, I found out that compressed files must be uncompressed in memory before reading, and that this restriction does not apply to raw files. However, when I try to parse the XML file from the res / raw folder using SAXParser, I get an IOException without any messages and stacktrace:

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(myXMLHandler);
InputSource src = new InputSource(stream);
xr.parse(src); // IOException

As far as I understand, these memory limits do not apply to source resources, but what causes this exception?

Here is the stacktrace from logcat:

11-11 23:47:50.729: WARN/System.err(4886): java.io.IOException
11-11 23:47:50.739: WARN/System.err(4886):     at android.content.res.AssetManager.readAsset(Native Method)
11-11 23:47:50.749: WARN/System.err(4886):     at android.content.res.AssetManager.access$800(AssetManager.java:36)
11-11 23:47:50.759: WARN/System.err(4886):     at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:542)
11-11 23:47:50.759: WARN/System.err(4886):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:504)
11-11 23:47:50.769: WARN/System.err(4886):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
11-11 23:47:50.779: WARN/System.err(4886):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
11-11 23:47:50.790: WARN/System.err(4886):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
+1
1

linux split -l 1 main.xml

private String readTxt()
{
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    for(int J=1;J<15;J++)
    {
        int i;
        try
        {
            InputStream raw = this.getAssets().open("xa"+J);    
            i = raw.read();
            while (i != -1)
            {
                byteArrayOutputStream.write(i);
                i = raw.read();
            }
            raw.close();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

SAX.

0

Source: https://habr.com/ru/post/1777025/


All Articles