import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; public class XmlReader { public static void main(String[] args) { new XmlReader().read(""); } public void read(String filePath) { filePath = "c://test.xml"; SAXBuilder builder = new SAXBuilder(false); try { Document doc = builder.build(filePath); Element root = doc.getRootElement(); List<Element> propertiesList = root.getChildren("properties"); System.out.println("properties的大小:"+propertiesList.size()); for(int i=0;i<propertiesList.size();i++){ List<Element> propertyList = propertiesList.get(i).getChildren("property"); System.out.println("property的大小:"+propertyList.size()); for(int j=0;j<propertyList.size();j++){ String name = propertyList.get(j).getAttributeValue("name"); System.out.println(name); String value = propertyList.get(j).getAttributeValue("value"); // String value = propertyList.get(j).getChildTextTrim("value"); System.out.println(value); propertyList.get(j).setAttribute("name", "hzq"); // propertyList.get(j).getChild("name").setText("hzq"); } } XMLOutputter outputter = new XMLOutputter(); outputter.output(doc, new FileOutputStream(filePath)); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }