XML文件如下:<?xml version="1.0" encoding="UTF-8"?>
<customers>
 <customer id="101" sex="man">
  <name>Tom</name>
  <age>12</age>
  <email>[email protected]</email>
 </customer>
 <customer id="102" sex="women">
  <name>Jerry</name>
  <age>22</age>
  <email>[email protected]</email>
 </customer>
</customers>DOM4J的代码如下:public class UpdateXML { public static void main(String[] args) {
// 1 取得Document对象
SAXReader reader = null;
File file = null;
Document document = null;

reader = new SAXReader();
file = new File("d:\\xml_document\\example.xml");
Element custEle = null;
List<Attribute> idAttrs = document.selectNodes("/customers/customer/@id"); //该行提示空指针错误。 try {
document = reader.read(file);
// 得到id=102的节点 for(int i = 0; i < idAttrs.size(); i++) {
String idValue = idAttrs.get(i).getValue();
if(idValue != null && idValue.equals("102")) {
custEle = idAttrs.get(i).getParent();
}
}
System.out.println(custEle);
// 为custEle添加一个子节点:<address></address>
Element address = custEle.addElement("address");
// 设置该节点文本值
address.setText("Shanghai");
// 设置子节点的属性
address.addAttribute("id", "021");

// 输出到XML
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");

XMLWriter writer = new XMLWriter(new FileWriter("d:\\xml_document\\example2.xml"), format); 
writer.write(document);
writer.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}

解决方案 »

  1.   

     List<Attribute> idAttrs = document.selectNodes("/customers/customer/@id");修改为:
    Element root = document.getRootElement();
     List<Attribute> idAttrs=root.elements("customer");
     for(int i = 0; i < idAttrs.size(); i++) {
     String idValue = idAttrs.get(i).attributeValue("id");}
      

  2.   


    Document document = null;
    reader = new SAXReader();
    file = new File("d:\\xml_document\\example.xml");
    楼主这里声明了document,还没有给document赋值。List<Attribute> idAttrs = document.selectNodes("/customers/customer/@id"); 
    这句,document就是null。
    file = new File("d:\\xml_document\\example.xml");
    document = reader.read(file);
      

  3.   

    没找着attributeValue这个方法。另外,上面的方法错在哪里了~~~
      

  4.   

     file = new File("d:\\xml_document\\example.xml");
            Element custEle = null;
            List<Attribute> idAttrs = document.selectNodes("/customers/customer/@id"); [color=#FF0000]//该行提示空指针错误。        try {
                document = reader.read(file);[/color]
    file =new File("d:\\xml_document\\example.xml");    
             try {    
                Document document = reader .read(file );    
                Element employees=document.getRootElement();    
                for(int i = 0; i < employees.size(); i++){    
                    String idValue = employees.get(i).getValue();
                    if(idValue != null && idValue.equals("102")) {
                        custEle = idAttrs.get(i).getParent();
                    }
                }catch(Exception e){
    .....
    }
    }    
     试试这个
      

  5.   

    你的代码逻辑顺序有问题应该是先读文件 后获得节点
    http://www.blogjava.net/junglesong/archive/2008/02/21/181196.html看看这个