<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(&#34; BatchItemFullInfoAnswer / Result / Item / ConfiguredItems / OtapiConfiguredItem / Configurators / ValuedConfigurator [@pid]&#34;);
先谢谢..
答案 0 :(得分:1)
在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();
}
}
}
答案 1 :(得分:1)
感谢vtd作者,你节省了我的一天和头痛。
<强>答案:强> 添加ConfiguredItems / OtapiConfiguredItem
while ((ii = itemQuantities.evalXPath()) != -1) {
productDetails.itemQuantitiesArrayList.add(itemQuantitiess);
}
//将valuesConfigurators添加到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);
}