简体   繁体   中英

java android very large xml parsing

I have got a very large xml file with categories in one xml file which maps to sub categories in another xml file according to category id. The xml file with only category id and names is loading fast, but the xml file which has subcategories with images path, description, latitude-longitude etc...is taking time to load.

I am using javax.xml package and org.w3c.dom package. The list action is loading the file in each click to look for subcategories. Is there any way to make this whole process faster?

Edit-1

Heres the code i am using to getch subcategories: Document doc = this.builder.parse(inStream, null);

        doc.getDocumentElement().normalize();
        NodeList pageList = doc.getElementsByTagName("page");
        final int length = pageList.getLength();

        for (int i = 0; i < length; i++)
        {
            boolean inCategory = false;
            Element categories = (Element) getChild(pageList.item(i), "categories");


            if(categories != null)
            {
                NodeList categoryList = categories.getElementsByTagName("category");
                for(int j = 0; j < categoryList.getLength(); j++)
                {
                    if(Integer.parseInt(categoryList.item(j).getTextContent()) == catID)
                    {
                        inCategory = true;
                        break;
                    }
                }
            }

            if(inCategory == true)
            {
                final NamedNodeMap attr = pageList.item(i).getAttributes();
                //

                //get Page ID
                final int categoryID = Integer.parseInt(getNodeValue(attr, "id"));

                //get Page Name
                final String categoryName = (getChild(pageList.item(i), "title") != null) ? getChild(pageList.item(i), "title").getTextContent() : "Untitled";

                //get ThumbNail
                final NamedNodeMap thumb_attr = getChild(pageList.item(i), "thumbnail").getAttributes();
                final String categoryImage = "placethumbs/" + getNodeValue(thumb_attr, "file");

                //final String categoryImage = "androidicon.png";

                Category category = new Category(categoryName, categoryID, categoryImage);

                this.list.add(category);
                Log.d(tag, category.toString());
            }
        }

使用基于SAX的解析器,DOM不适用于大型xml。

Maybe a SAX processor would be quicker (assuming your App is slowing down due to memory requirements of using a DOM-style approach?)

Article on processing XML on android

SOF question about SAX processing on Android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM