dom4j使用selectNodes搜索xml节点

第一步:引用jar包

   dom4j-1.6.1.jar (http://www.dom4j.org/下载最新版)

   jaxen-1.1-beta-6.jar (http://jaxen.codehaus.org/ 这是官网,点击下载链接好像不行,可以去csdn)

   支持XPath必须的,否则会报错java.lang.NoClassDefFoundError: org/jaxen/JaxenException

第二步:XPath基本语法

   http://www.w3school.com.cn/xpath/index.asp

第三步:实例

users.xml


- < Users >
- < User id =" 0 " >
  < Name > youchuancong 
  < Age > 23 
  
- < User id =" 1 " >
  < Name > cindy 
  < Age > 23 
  
  

XMLTools.java  
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLTools {
public void creat(String filename){//创建xml文件
 Document document = DocumentHelper.createDocument();
    Element usersElement = document.addElement("Users");
   
    Element userElement =  usersElement.addElement("User");
    userElement.addAttribute("id", "0");
    
    Element nameElement=userElement.addElement("Name");
    nameElement.setText("youchuancong");
    
    Element  ageElement=userElement.addElement("Age");
    ageElement.setText("23");
    
    Element userElement1 =  usersElement.addElement("User");
    userElement1.addAttribute("id", "1");
    
    Element nameElement1=userElement1.addElement("Name");
    nameElement1.setText("cindy");
    
    Element  ageElement1=userElement1.addElement("Age");
    ageElement1.setText("23");
    
   try{
   XMLWriter output = new XMLWriter(
           new FileWriter( new File(filename) ));
       output.write( document );
       output.close();
       }
    catch(IOException e){System.out.println(e.getMessage());}
}

public void read(String filename,String id) throws Exception{//根据id读出学生姓名
 SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(new File(filename));
    Element e = (Element)document.selectSingleNode("/Users/User[@id='"+id+"']");
    Element name = e.element("Name");
    System.out.println("Name:"+name.getText());
 
}
public void readName(String filename) throws Exception{//读出所有学生的学生姓名
 SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(new File(filename));
    List users = document.selectNodes("//Name");
    Iterator it = users.iterator();
    while(it.hasNext()){
     Element e = (Element)it.next();
     System.out.println("Name:"+e.getText());
    }
}
public void addUser(String filename,String id,String name,String age) throws Exception{
 SAXReader saxReader = new SAXReader();//添加信息
    Document document = saxReader.read(new File(filename));
    Element e = (Element)document.selectSingleNode("/Users");
  // Element e =  document.getRootElement();
   Element user = e.addElement("User");
   user.setAttributeValue("id", id);
   user.addElement("Name").setText(name);
   user.addElement("Age").setText(age);
     XMLWriter output = new XMLWriter(
             new FileWriter( new File(filename) ));
         output.write( document );
         output.close();
}
public void readUser(String filename) throws Exception{//读出所有学生的基本信息
 SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(new File(filename));
    List l = document.selectNodes("/Users/User");
    Iterator it = l.iterator();
    while(it.hasNext()){
     Element e = (Element)it.next();
     System.out.println("id:"+e.attributeValue("id"));
     Element name = e.element("Name");
     System.out.println("name:"+name.getText());
     Element age = e.element("Age");
     System.out.println("age:"+age.getText());
     System.out.println("#############################");
    }
}
public void delete(String filename,String id) throws Exception{//删除指定id的学生
 SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(new File(filename));
    Element e = (Element)document.selectSingleNode("/Users/User[@id='"+id+"']");
    if(e!=null){
     Element parent = e.getParent();
     parent.remove(e);
     
      XMLWriter output = new XMLWriter(
               new FileWriter( new File(filename) ));
          output.write( document );
          output.close();
    }
}
public void readAll(String filename) throws Exception{//选取所有节点
 SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(new File(filename));
    List users = document.selectNodes("//*");
    Iterator it = users.iterator();
    while(it.hasNext()){
     Element e = (Element)it.next();
     System.out.println("ElementName:"+e.getName()+"||value:"+e.getText());
    }
}
public void modify(String filename,String id,String name) throws Exception{//修改指定学号的学生姓名
 SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(new File(filename));
    Element e = (Element)document.selectSingleNode("/Users/User[@id='"+id+"']");
    Element n = e.element("Name");
    n.setText(name);
     XMLWriter output = new XMLWriter(
             new FileWriter( new File(filename) ));
         output.write( document );
         output.close();     
}
}

XMLTest.java 
public class XMLTest { 
 public static void main(String[] args) {
  XMLTools xml = new XMLTools();
  //xml.creat("users.xml");
  try {
   //xml.read("users.xml", "0");
   //xml.addUser("users.xml", "4", "no", "100");
   //xml.readUser("users.xml");
   //xml.readName("users.xml");
   xml.readAll("users.xml");
   //xml.modify("users.xml", "0", "helloworld");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

我在调用selectNodes方法的时候,碰到了一些问题,
String xPath = "//extensionPoint[@name='" + extName + "']";
List eles = doc.selectNodes(xPath);
上面不加//,得到的eles是一个空的list。如果是根节点可以不加//,这里在w3school有说明的

感谢原博客作者
http://blog.csdn.net/youycc/article/details/5736103

你可能感兴趣的:(java,java,xml)