public class DomTest { public static void main(String[] args) { testXml(); } public static void testXml() { try { // 得到DOM解析器的工厂实例 DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance(); // 从DOM工厂中获得DOM解析器 DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder(); // 把要解析的xml文档读入DOM解析器 Document doc = dbBuilder.parse("src/before.xml"); // System.out.println("处理该文档的DomImplementation对象 = " // + doc.getImplementation()); // 得到文档名称为Student的元素的节点列表 // NodeList nList = doc.getElementsByTagName("object"); // Element node = (Element) nList.item(0); // NodeList nodeList = node.getElementsByTagName("aspect"); NodeList nodeList = ((Element)doc.getElementsByTagName("object").item(0)).getElementsByTagName("aspect"); for (int j = 0; j < nodeList.getLength(); j++) { Element n1 = (Element) nodeList.item(j); System.out.println(n1.getAttribute("name")); System.out.println(n1.getFirstChild().getNodeValue()); } } catch (Exception e) { e.printStackTrace(); } } public static void queryXml() { try { // 得到DOM解析器的工厂实例 DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance(); // 从DOM工厂中获得DOM解析器 DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder(); // 把要解析的xml文档读入DOM解析器 Document doc = dbBuilder.parse("src/school.xml"); System.out.println("处理该文档的DomImplementation对象 = " + doc.getImplementation()); // 得到文档名称为Student的元素的节点列表 NodeList nList = doc.getElementsByTagName("Student"); // 遍历该集合,显示结合中的元素及其子元素的名字 for (int i = 0; i < nList.getLength(); i++) { Element node = (Element) nList.item(i); System.out.println("Name: " + node.getElementsByTagName("Name").item(0) .getFirstChild().getNodeValue()); System.out.println("Num: " + node.getElementsByTagName("Num").item(0) .getFirstChild().getNodeValue()); System.out.println("Classes: " + node.getElementsByTagName("Classes").item(0) .getFirstChild().getNodeValue()); System.out.println("Address: " + node.getElementsByTagName("Address").item(0) .getFirstChild().getNodeValue()); System.out.println("Tel: " + node.getElementsByTagName("Tel").item(0) .getFirstChild().getNodeValue()); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static void testSample() { // TODO Auto-generated method stub // 创建一个DocumentBuilderFactory的对象 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 创建一个DocumentBuilder的对象 try { // 创建DocumentBuilder对象 DocumentBuilder db = dbf.newDocumentBuilder(); // 通过DocumentBuilder对象的parser方法加载books.xml文件到当前项目下 Document document = db.parse("src/Students.xml"); // 获取所有Student节点的集合 NodeList StudentList = document.getElementsByTagName("Student"); // 通过nodelist的getLength()方法可以获取StudentList的长度 System.out.println("DOM解析开始..."); // 遍历每一个Student节点 for (int i = 0; i < StudentList.getLength(); i++) { System.out.println("开始解析第" + (i + 1) + "个学生"); // 通过 item(i)方法 获取一个Student节点,nodelist的索引值从0开始 Node book = StudentList.item(i); // 获取Student节点的所有属性集合 NamedNodeMap attrs = book.getAttributes(); // 遍历Student的属性 for (int j = 0; j < attrs.getLength(); j++) { // 通过item(index)方法获取Student节点的某一个属性 Node attr = attrs.item(j); // 输出学生的属性名和属性值 System.out.println(attr.getNodeName() + ":" + attr.getNodeValue()); } NodeList childNodes = book.getChildNodes(); // 遍历childNodes获取每个节点的节点名和节点值 for (int k = 0; k < childNodes.getLength(); k++) { // 区分出text类型的node以及element类型的node if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) { // 输出子节点的属性名和属性值 System.out.println(childNodes.item(k).getNodeName() + ":" + childNodes.item(k).getFirstChild() .getNodeValue()); } } } } catch (Exception e) { e.printStackTrace(); } System.out.println("DOM解析结束..."); } }
<?xml version = "1.0" encoding = "utf-8"?> <School> <Student> <Name>沈浪</Name> <Num>1006010022</Num> <Classes>信管2</Classes> <Address>浙江杭州3</Address> <Tel>123456</Tel> </Student> <Student> <Name>沈1</Name> <Num>1006010033</Num> <Classes>信管1</Classes> <Address>浙江杭州4</Address> <Tel>234567</Tel> </Student> <Student> <Name>沈2</Name> <Num>1006010044</Num> <Classes>生工2</Classes> <Address>浙江杭州1</Address> <Tel>345678</Tel> </Student> <Student> <Name>沈3</Name> <Num>1006010055</Num> <Classes>电子2</Classes> <Address>浙江杭州2</Address> <Tel>456789</Tel> </Student> </School>