如何从选择路径获取属性值或选择属性ximpleware [英] How to get Attribute values from Select path or select Attribute ximpleware

查看:225
本文介绍了如何从选择路径获取属性值或选择属性ximpleware的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <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>

以上是我的XML。 。我需要获取每个 OtapiConfiguredItem的 ValuedConfigurator PID和VID属性值

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

我试过选择路径vcPId.selectXPath (BatchItemFullInfoAnswer / Result / Item / ConfiguredItems / OtapiConfiguredItem / Configurators / ValuedConfigurator [@pid]);

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

在此先感谢..

推荐答案

在vtd-xml中有很多方法可以做到这一点..我给你一些选择

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

第一个不使用XPath来获取属性,但调用游标api的getAttrVal来执行此操作...

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

第二个使用完整的XPath,阅读代码中嵌入的注释

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

这篇关于如何从选择路径获取属性值或选择属性ximpleware的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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