50,528
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
try {
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File("e:/user.xml"));
List list = doc.selectNodes("/users/user//@state");
System.out.println(list.size());
Iterator iter = list.iterator();
System.out.println(iter.hasNext());
while(iter.hasNext()){
Attribute attribute = (Attribute)iter.next();
System.out.println(attribute);
System.out.println(attribute.getValue());
if(attribute.getValue().equals("owner")){
attribute.setValue("other");
}
System.out.println(attribute.getValue());
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter output = new XMLWriter(new FileWriter(new File("e:/"
+ "user" + ".xml")), format);
output.write(doc);
output.close();