如何使用ximpleware在XML中获取Inner数组子元素

时间:2016-04-19 07:21:33

标签: java xml vtd-xml

AutoPilot searchProperties = new AutoPilot();
searchProperties.selectXPath("/BatchItemSearchResultAnswer/Result/SearchProperties/Content/Item");
        searchProperties.bind(nav);
        searchPro searchOption = new searchPro();
        while (searchProperties.evalXPath() != -1) {
            //reuser
            name.resetXPath();
            name.selectXPath("Name");
            searchOption.id = Id.evalXPathToString();
            searchOption.name = name.evalXPathToString();
            Log.e("SearchId", Id.evalXPathToString());
            Log.e("SearchName", name.evalXPathToString());
            Image.resetXPath();
            Image.selectXPath("/BatchItemSearchResultAnswer/Result/SearchProperties/Content/Item/Values/PropertyValue");
            searchOption.propertyList = new ArrayList();
            while (Image.evalXPath() != -1) {
                property pro = new Property();
                pro.id = Id.evalXPathToString();
                Log.e("SearchPId", Id.evalXPathToString());
                Log.e("SearchPName", name.evalXPathToString());
                searchOption.propertyList.add(pro);
            }
       }

这是我的XML

<SearchProperties><Content> <Item><Id>12345</Id><Name>scene</Name><Values><PropertyValue><Id>29</Id><Name>Le</Name></PropertyValue>    <PropertyValue><Id>208</Id><Name>Business</Name></PropertyValue>  </Values></Item><Item>..</Item></Content></SearchProperties>

SearchProperties while循环只有一次。当我删除I​​mage.selectPath(“”)行。那个时候它会正确循环。 怎么解决这个问题。

先谢谢。

2 个答案:

答案 0 :(得分:1)

您正在嵌套XPath评估...尽可能将XPath选择移出while循环总是更好。原因是编译XPath相对来说是一个缓慢的操作......因为它需要构建XPath数据结构,执行各种优化来加速xpath评估......

手动导航在您的用例中可能更有效,因为您已表明只想在XML树上循环一次...

此外,您在另一个相关的绝对XPath中嵌套了一个绝对XPath,它对性能非常不利,并且没有意义......我将图像对象的xpath绑定更改为相对xpath“values/propertyValue”。

总体目标是减少浪费的计算并最大限度地重复使用。关于XPath重用,正如您所知,XPath评估有4种方法...... evalXPath()evalXPathToString()evalXPathToBoolean()evalXPathToNumber()。在这4种方法中,只有evalXPath()需要resetXPath()才能进行后续重用...其他3调用resetXPath ...因此不需要在每次调用后调用resetXPath()。

  AutoPilot searchProperties = new AutoPilot();
AutoPilot image = new AutoPilot(vn);
AutoPilot name = new AutoPilot(vn);
name.selectXPath("Name");
Image.selectXPath("values/PropertyValue");// should use relative path here, makes a huge difference in performance
 searchProperties.selectXPath("/BatchItemSearchResultAnswer/Result/SearchProperties/Content/Item");


      searchProperties.bind(nav);
        searchPro searchOption = new searchPro();
        while (searchProperties.evalXPath() != -1) {
            //reuser
            //name.resetXPath();

            searchOption.id = Id.evalXPathToString();
            searchOption.name = name.evalXPathToString();
            Log.e("SearchId", Id.evalXPathToString());
            Log.e("SearchName", name.evalXPathToString());


            searchOption.propertyList = new ArrayList();
            vn.push()
            while (Image.evalXPath() != -1) {
                property pro = new Property();
                pro.id = Id.evalXPathToString();
                Log.e("SearchPId", Id.evalXPathToString());
                Log.e("SearchPName", name.evalXPathToString());
                searchOption.propertyList.add(pro);
            }
            Image.resetXPath();
            vn.pop();
       }

答案 1 :(得分:0)

最后花了一整天才得到结果。

if (navTemp.toElement(VTDNav.FC, "SearchProperties")) {
                            Log.e("Search", "SearchProperties");
                            if (navTemp.toElement(VTDNav.FC, "Content")) {
                                Log.e("Search", "Content");
                                if (navTemp.toElement(VTDNav.FC, "Item")) {
                                    do {
                                        SearchPropertiesItems item = new SearchPropertiesItems();
                                        if (navTemp.toElement(VTDNav.FC)) {
                                            Log.e("Search", "Item");
                                            do {

                                                if (!navTemp.matchElement("Values")) {
                                                    if (navTemp.toString(navTemp.getCurrentIndex()).equalsIgnoreCase("id")) {
                                                        item.id = navTemp.toString(navTemp.getText());
                                                    } else {
                                                        item.name = navTemp.toString(navTemp.getText());
                                                    }
                                                    //Log.e("SearchItem", navTemp.toString(navTemp.getCurrentIndex()) + "==>" + navTemp.toString(navTemp.getText()));
                                                } else {
                                                    //if (navTemp.toElement(VTDNav.FC, "Values")) {

                                                    if (navTemp.toElement(VTDNav.FC, "PropertyValue")) {
                                                        do {
                                                            SearchPropertiesProperty property = new SearchPropertiesProperty();
                                                            if (navTemp.toElement(VTDNav.FC)) {
                                                                do {

                                                                    if (navTemp.toString(navTemp.getCurrentIndex()).equalsIgnoreCase("id")) {
                                                                        property.id = navTemp.toString(navTemp.getText());
                                                                    } else {
                                                                        property.name = navTemp.toString(navTemp.getText());
                                                                    }

                                                                    // Log.e("SearchItem >>", navTemp.toString(navTemp.getCurrentIndex()) + "==>" + navTemp.toString(navTemp.getText()));
                                                                }
                                                                while (navTemp.toElement(VTDNav.NEXT_SIBLING));
                                                                navTemp.toElement(VTDNav.P);
                                                                property.ItemId = item.id;

                                                                item.properties.add(property);
                                                            }
                                                        }
                                                        while (navTemp.toElement(VTDNav.NEXT_SIBLING));
                                                        navTemp.toElement(VTDNav.P);
                                                    }
                                                    // }
                                                }

                                            } while (navTemp.toElement(VTDNav.NEXT_SIBLING));
                                            navTemp.toElement(VTDNav.P);
                                            productList.searchPropertiesItems.add(item);
                                        }
                                    } while (navTemp.toElement(VTDNav.NEXT_SIBLING));
                                }
                            }
                        } 

参考链接: http://vtd-xml.sourceforge.net/codeSample/cs5.html http://www.javaworld.com/article/2071745/soa/simplify-xml-processing-with-vtd-xml.html