日期:2014-05-20 浏览次数:20633 次
<?xml version="1.0" encoding="UTF-8"?> <users> <user> <id>1</id> <username>admin</username> <userpass>123456</userpass> </user> <user> <id>2</id> <username>zhang</username> <userpass>123456</userpass> </user> </users>
import java.io.*; import java.util.*; import org.w3c.dom.*; import javax.xml.parsers.*; public class D { public static void main(String arge[]) { long lasting = System.currentTimeMillis(); try { File f = new File("D://test.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(f); NodeList nl = doc.getElementsByTagName("user"); for (int i = 0; i < nl.getLength(); i++) { System.out.print("ID:" + doc.getElementsByTagName("id").item(i).getFirstChild().getNodeValue()); System.out.println("username:" + doc.getElementsByTagName("username").item(i).getFirstChild().getNodeValue()); System.out.println("userpass:" + doc.getElementsByTagName("userpass").item(i).getFirstChild().getNodeValue()); } } catch (Exception e) { e.printStackTrace(); } } }