【问题标题】:java android very large xml parsingjava android非常大的xml解析
【发布时间】:2011-09-07 10:41:15
【问题描述】:

我有一个非常大的 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());
            }
        }

【问题讨论】:

  • 向我们展示访问子类别的代码

标签: java android xml parsing


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    也许 SAX 处理器会更快(假设您的应用程序由于使用 DOM 样式方法的内存需求而变慢?)

    Article on processing XML on android

    SOF question about SAX processing on Android

    【讨论】:

    • woodstox 怎么样?我在这里在 stackoverflow 中读到它的速度相当快.. 那是我要找的东西吗?
    猜你喜欢
    • 2014-09-28
    • 2012-10-22
    • 2010-09-26
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多