How to get Attribute values from Select path or select Attribute ximpleware

送分小仙女□ 提交于 2019-12-06 18:57:25

There are many ways to do this in vtd-xml.. I give you a few options

First one does not use XPath to get to the attribute, but calls the cursor api's getAttrVal to do so...

import com.ximpleware.*;
public class queryAttr {
    public static void main(String[] s) throws VTDException{
        VTDGen vg = new VTDGen();
        vg.selectLcDepth(5);// improve XPath performance for deep document
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/BatchItemFullInfoAnswer/Result/Item/ConfiguredItems/OtapiConfiguredItem/Configurators/ValuedConfigurator");
        int i=0,j=0;
        while((i=ap.evalXPath())!=-1){
            j= vn.getAttrVal("pid");
            if (j!=-1)
                System.out.println(" attr value for pid is ==>"+vn.toString(j));
            j= vn.getAttrVal("vid");
            if (j!=-1)
                System.out.println(" attr value for vid is ==>"+vn.toString(j));
        }
    }
}

Second one uses full XPath, read the comments embedded in the code

import com.ximpleware.*;
public class queryAttr {
    public static void main(String[] s) throws VTDException{
        VTDGen vg = new VTDGen();
        vg.selectLcDepth(5);// improve XPath performance for deep document
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/BatchItemFullInfoAnswer/Result/Item/ConfiguredItems/OtapiConfiguredItem/Configurators/ValuedConfigurator/@pid");
        AutoPilot ap2 = new AutoPilot(vn);
        ap2.selectXPath("../@vid");
        int i=0,j=0;
        while((i=ap.evalXPath())!=-1){
            System.out.println(" attr value for pid is ==>"+vn.toString(i+1)); // notice this is i+1, not i cuz i is the vtd record index for pid
            vn.push();// maintain consistency of autoPilot with push/pop combo
            if ((j=ap2.evalXPath())!=-1)
                System.out.println(" attr value for vid is ==>"+vn.toString(j+1)); // notice this is j+1, not j cuz j is the vtd record index for vid
            vn.pop();
        }
    }
}

Thanks vtd author, You save my day and headache.

Answer: adding ConfiguredItems/OtapiConfiguredItem

while ((ii = itemQuantities.evalXPath()) != -1) {
productDetails.itemQuantitiesArrayList.add(itemQuantitiess);
}

// adding valuedConfigurators to OtapiConfiguredItem arraylist

AutoPilot valuedConfigurators = new AutoPilot();
     valuedConfigurators.selectXPath("/BatchItemFullInfoAnswer/Result/Item/ConfiguredItems/OtapiConfiguredItem/Configurators/ValuedConfigurator");
            valuedConfigurators.bind(nav);
 int b = 0;
       String color = "";// items names
                ProductDetails.ItemQuantities configuredItem = null;
                int pos = 0;// index for arraylist
                int valueIndex = 0;//0-color, 1->size
                while ((valuedConfigurators.evalXPath()) != -1) {
                    {
                        if (valueIndex == 0) {
                            StringBuilder builder = new StringBuilder();
                            //color
                            configuredItem = productDetails.itemQuantitiesArrayList.get(pos);
                            pos += 1;
                            b = nav.getAttrVal("Pid");
                            if (b != -1) {
                                builder.append(nav.toString(b));

                            }
                            //  Log.e("Configure pid----->: ", nav.toString(b));
                            // System.out.println(" attr value for pid is ==>" + nav.toString(b));
                            b = nav.getAttrVal("Vid");
                            if (b != -1)
                                if (b != -1) {
                                    builder.append(",");
                                    builder.append(nav.toString(b));

                                }
                            color = builder.toString();
                            valueIndex++;
                          /*  Log.e("Configure vid----->: ", nav.toString(b));
                            System.out.println(" attr value for vid is ==>" + nav.toString(b));*/
                        } else {
                            StringBuilder builder = new StringBuilder();
                            //size
                            b = nav.getAttrVal("Pid");
                            if (b != -1) {
                                builder.append(nav.toString(b));

                            }
                            // System.out.println(" attr value for pid is ==>" + nav.toString(b));
                            b = nav.getAttrVal("Vid");
                            if (b != -1) {
                                builder.append(",");
                                builder.append(nav.toString(b));

                            }
                            // System.out.println(" attr value for vid is ==>" + nav.toString(b));
                            configuredItem.color = color;
                            configuredItem.size = builder.toString();
                            ;
                            valueIndex = 0;
                            Log.e("Configure:", "id:" + configuredItem.iqId + ",color:" + configuredItem.color + ",size:" + configuredItem.size);
                        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!