import java.io.*; import org.w3c.dom.*; import javax.xml.parsers.*; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class q { public static void main(String[] args) { xml(); pro(); } private static void xml(){ try { File f=new File("WebRoot/MyXml.xml"); DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc = builder.parse(f); NodeList nl = doc.getElementsByTagName("user"); doc.getElementsByTagName("user.name").item(1).getFirstChild().setNodeValue("Thanks");//修改 for (int i=0;i<nl.getLength();i++){ System.out.print("用户名:" + doc.getElementsByTagName("user.name").item(i).getFirstChild().getNodeValue()); System.out.println(" 密码:" + doc.getElementsByTagName("user.pwd").item(i).getFirstChild().getNodeValue()); } } catch (Exception e) { System.out.println("错误"+e.getMessage()); } } private static String param1; private static String param2; public static void pro(){ Properties prop = new Properties(); //InputStream in = Object.class.getResourceAsStream("/lognin.properties"); try { FileInputStream inputfile=new FileInputStream("WebRoot/lognin.properties");//读取properties文件 prop.load(inputfile); //加载 prop.setProperty("name", "admin");//修改键中的值 param1 = prop.getProperty("name").trim(); //读取键值 param2 = prop.getProperty("pwd").trim(); System.out.println(param1); System.out.println(param2); } catch (IOException e) { e.printStackTrace(); } } }