gpt4 book ai didi

java - 如何从选择路径或选择属性 ximpleware 中获取属性值

转载 作者:行者123 更新时间:2023-11-29 19:57:10 25 4
gpt4 key购买 nike

 <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 属性值

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

提前致谢..

最佳答案

在 vtd-xml 中有很多方法可以做到这一点。我给你几个选项

第一个不使用 XPath 来获取属性,而是调用游标 api 的 getAttrVal 来这样做...

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,阅读代码中嵌入的注释

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

关于java - 如何从选择路径或选择属性 ximpleware 中获取属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36641784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com