java android非常大的xml解析

时间:2011-09-07 10:41:16

标签: java android xml parsing

我有一个非常大的xml文件,其中包含一个xml文件中的类别,该文件根据类别ID映射到另一个xml文件中的子类别。仅具有类别ID和名称的xml文件正在快速加载,但具有图像路径,描述,纬度 - 经度等子类别的xml文件正在加载时间。

我使用的是javax.xml包和org.w3c.dom包。 列表操作是在每次单击时加载文件以查找子类别。 有没有办法让整个过程更快?

修改-1

这是我用来获取子类别的代码: 文档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());
            }
        }

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

也许SAX处理器会更快(假设你的应用程序由于使用DOM风格的方法的内存要求而变慢?)

Article on processing XML on android

SOF question about SAX processing on Android