XPath xpath = XPath.newInstance("//BuildID");
Element ele = (Element)xpath.selectSingleNode(doc);
String str = ele.getText();//str就是你要的
Element ele = (Element)xpath.selectSingleNode(doc);
String str = ele.getText();//str就是你要的
java.lang.NullPointerException
at org.ct.anthor.Test.main(Test.java:18)
String name;
public static void main(String[] args) {
SAXBuilder sb = new SAXBuilder();
try {
Document doc=sb.build("G:\\Sample1.xml");
Element root=doc.getRootElement();
XPath xpath = XPath.newInstance("//BuildID");
Element ele = (Element)xpath.selectSingleNode(doc);
String str = ele.getText();
System.out.print(str);
}catch(Exception e){
e.printStackTrace();
}
//System.out.print(t.getName());
}
就是这个不是这个没有selectNode这个函数的
比如你有这么一个maodie.xml文件<?xml version="1.0" encoding="UTF-8"?>
<book>
<name type="math">
maodieme
</name>
<name type="math2">
maodieme3
</name>
<sex type="math3">
male
</sex>
</book>那么你可以获得数据 XMLConfiguration config=new XMLConfiguration("maodie.xml");
String s=config.getString("name(1)[@type]");//获得第二个name孩子的type属性的值
String ss=config.getString("name(0)");//获取第一个name孩子的值
String sss=config.getString("name(1)");//获取第二个name孩子的值
String ssss=config.getString("sex");//获取名为sex孩子的值
System.out.println(s);
System.out.println(ss);
System.out.println(sss);
System.out.println(ssss);
你也许会觉得很麻烦..真正试了才知道什么叫爽
楼主好运
//我测试过了 这样就可以取到了
public static void readFile(File inputXml)
{
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
HashMap xmlMap = new HashMap();
xmlMap.put("mo","xmlns://www.fortifysoftware.com/schema/fvdl");
XPath x = document.createXPath("//mo:BuildID");
x.setNamespaceURIs(xmlMap);
Element value = (Element)x.selectSingleNode(document);
System.out.println(value.getText());
} catch (DocumentException e)
{
e.printStackTrace();
}
}