如何使用ximpleware获取XML中的内部数组子元素 [英] How to get Inner array child element in XML using ximpleware

查看:124
本文介绍了如何使用ximpleware获取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,而仅循环一次.当我删除I​​mage.selectPath(")行时.到时它将正确循环. 如何解决这个问题.

在此先感谢.

解决方案

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

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

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

总体目标是减少浪费的计算并最大程度地提高重用性.如您所知,关于XPath重用,有4种方法用于XPath评估... 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();
       }

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);
            }
       }

here is my 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 loop only one time . when i remove Image.selectPath("") line. that time it will loop correctly. how to solve this.

Thanks in Advance.

解决方案

You are nesting XPath evaluations... it is always better to move XPath selection out of a while loop whenever possible. The reason is that compile XPath is relatively speaking a slow operation... as it needs to construct XPath data structure, perform various optimization that speeds up xpath evaluation...

Manual navigation might be more efficient in your use case as you have indicated that you want to loop over XML tree only once...

Also you nested one absolute XPath inside another related absolute XPath, it is very bad for performance, and does not really make sense... I changed the image object's xpath binding to a relative xpath "values/propertyValue."

The overall goal is to reduced wasteful computation and maximize reuse. Concerning XPath reuse, as you are aware there are 4 methods for XPath evaluation... evalXPath(), evalXPathToString(), evalXPathToBoolean(), evalXPathToNumber(). Of those 4 methods, only evalXPath() requires resetXPath() for subsequent reuse... other 3 call resetXPath internally..and thus do not require you to call resetXPath() after each invocation.

  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();
       }

这篇关于如何使用ximpleware获取XML中的内部数组子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆