如何从 Select 路径或 select 属性 ximpleware 获取属性值
How to get Attribute values from Select path or select Attribute 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属性值=13=]
我尝试了 Select 路径 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();
}
}
}
感谢 vtd 作者,您让我省心省力。
答案:
添加 ConfiguredItems/OtapiConfiguredItem
while ((ii = itemQuantities.evalXPath()) != -1) {
productDetails.itemQuantitiesArrayList.add(itemQuantitiess);
}
// 添加 valuedConfigurators 到 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);
}
<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属性值=13=]
我尝试了 Select 路径 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();
}
}
}
感谢 vtd 作者,您让我省心省力。
答案: 添加 ConfiguredItems/OtapiConfiguredItem
while ((ii = itemQuantities.evalXPath()) != -1) {
productDetails.itemQuantitiesArrayList.add(itemQuantitiess);
}
// 添加 valuedConfigurators 到 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);
}