1、xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <info> <path>localhost</path> <database>XHIS</database> <username>sa</username> <password>sa</password> </info> 2、java操作类如下: package com.test.xml; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class ReadXml { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { new ReadXml().test(); } public void test()throws ParserConfigurationException, SAXException, IOException{ InputStream is = this.getClass().getClassLoader().getResourceAsStream("com/test/xml/test.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc= (Document) builder.parse(is); doc.normalize(); //第一种,根据具体节点解析 String str = doc.getElementsByTagName("path").item(0).getFirstChild().getNodeValue(); System.out.println("str---="+str); //第二种,层层解析 NodeList nodeList = doc.getElementsByTagName("Info"); Element element0 = (Element)nodeList.item(0); NodeList childList = element0.getChildNodes(); if(childList!=null && childList.getLength()>0){ for(int i=0;i<childList.getLength();i++){ Node node = childList.item(i); if(node instanceof Element){ String nodeName = node.getNodeName(); String value = childList.item(i).getFirstChild().getNodeValue(); System.out.println("nodeName="+nodeName+",value="+value); } } } } }