background image

try
{
document=builder.build(new FileInputStream(xmlPath));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
JDomSearchTest j=new JDomSearchTest();
Element root=j.document.getRootElement();
List list=null;
try
{
//下面进行从得到的 root 下面的/student/user 开始查找
//此的/student/user 就相当于我们常见的目录地址一样
//因为 XML 的组织形式本来就是树形的
      list = XPath.selectNodes(root,"/student/user");
}
catch (JDOMException e)
{
e.printStackTrace();
}
for(int i=0;i<list.size();i++)
{
Element e=(Element)list.get(i);
//取得属性
      String id=e.getAttributeValue("id");
try
{
//利用属性进行查寻
//得到对应 id 下面的 name 的值
 

 

 

 

 

 

 

 

String

 

name

 

((Text)XPath.selectSingleNode(e,"//user[@id="+id+"]/name/text()"))

.getTextNormalize();
//得到对应 id 下面的 age 的值
 

 

 

 

 

 

 

 

String 

age=((Text)XPath.selectSingleNode(e,"//user[@id="+id+"]/age/text()"))