XML:
<Document xmlns="urn:abc:xyz">
<A>
<B>
</B>
</A>
需要获取xmlns的值,即urn:abc:xyz。我是vtd-xml的新手,只是通过阅读一些帖子来尝试。下面是我到目前为止所做的。
String testData = context.getXmlFragment().toString().trim();
final VTDGen vg = new VTDGen();
vg.setDoc(testData.getBytes());
try {
vg.parse(true);
VTDNav vn = vg.getNav();
final AutoPilot apParty = new AutoPilot(vn);
apParty.selectXPath("/Document");
int i = -1;
while ((i = apParty.evalXPath()) != -1) {
long l = vn.getContentFragment();
System.out.println(" -==> " + vn.toString((int) l, (int) (l >> 32)));
}
}catch (XPathEvalException e) {
e.printStackTrace();
}
最佳答案
这是在XPath中执行名称空间节点查询的代码...
import com.ximpleware.*;
public class queryNS {
public static void main(String[] args) throws VTDException {
// TODO Auto-generated method stub
VTDGen vg = new VTDGen();
String xml = "<test xmlns='xyz'><a/></test>";
byte[] ba = xml.getBytes();
vg.setDoc(ba);
vg.parse(true);
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/test/namespace::node()");
int i=0;
while((i=ap.evalXPath())!=-1){
System.out.println(" value ===>"+vn.toString(i+1));
}
}
}