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

半世苍凉 提交于 2020-01-23 17:56:07

问题


 <ConfiguredItems>
    <OtapiConfiguredItem>
      <Id>3117283038955</Id>
      <Quantity>1693</Quantity>
      <Configurators>
        <ValuedConfigurator Pid="1627207" Vid="3232480" />
        <ValuedConfigurator Pid="20509" Vid="28314" />
      </Configurators>
    </OtapiConfiguredItem>
    <OtapiConfiguredItem>
      <Id>3117283038956</Id>
      <Quantity>1798</Quantity>
      <Configurators>
        <ValuedConfigurator Pid="1627207" Vid="3232480" />
        <ValuedConfigurator Pid="20509" Vid="6145171" />
      </Configurators>
</OtapiConfiguredItem>
    <OtapiConfiguredItem>
      <Id>3117283038957</Id>
      <Quantity>1815</Quantity>
        <Configurators>
          <ValuedConfigurator Pid="1627207" Vid="28331" />
          <ValuedConfigurator Pid="20509" Vid="28315" />
        </Configurators>
  </OtapiConfiguredItem>

The above is my XML .. I need to get ValuedConfigurator PID and VID attribute values for each OtapiConfiguredItem

I tried Select path vcPId.selectXPath("BatchItemFullInfoAnswer/Result/Item/ConfiguredItems/OtapiConfiguredItem/Configurators/ValuedConfigurator[@pid]");

Thanks in Advance..


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/36641784/how-to-get-attribute-values-from-select-path-or-select-attribute-ximpleware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!