结合在网上看到的实例,总结出下面两个简单的例子。关于各个方法的说明可以参见各Java Doc
xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <HD> <disk name="C"> <capacity>8G</capacity> <directories>200</directories> <files>1580</files> </disk> <disk name="D"> <capacity>10G</capacity> <directories>500</directories> <files>3000</files> </disk> </HD>
第一种方法:使用JDK提供的类库来实现该功能
package com.test; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class test { public static void main(String[] args) throws Exception { DocumentBuilderFactory docBuilderFac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFac.newDocumentBuilder(); Document doc = docBuilder.parse(new File("D://test.xml")); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("disk"); for(int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if(childNode.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) childNode; String dirveName = element.getAttributes().getNamedItem("name").getTextContent(); NodeList lastNodeList = element.getElementsByTagName("capacity"); String capacity = lastNodeList.item(0).getChildNodes().item(0).getNodeValue(); NodeList lastNodeList2 = element.getElementsByTagName("directories"); String directories = lastNodeList2.item(0).getChildNodes().item(0).getNodeValue(); NodeList lastNodeList3 = element.getElementsByTagName("files"); String files = lastNodeList3.item(0).getChildNodes().item(0).getNodeValue(); System.out.println("磁盘信息:"); System.out.println("分区盘符:"+dirveName); System.out.println("分区容量:"+capacity); System.out.println("目录数:"+directories); System.out.println("文件数:"+files); System.out.println("-----------------------------------"); } } } }
输出结果为:
磁盘信息:
分区盘符:C
分区容量:8G
目录数:200
文件数:1580
-----------------------------------
磁盘信息:
分区盘符:D
分区容量:10G
目录数:500
文件数:3000
-----------------------------------
第二种方法:使用JDOM来实现
package com.test; import java.io.File; import java.io.File; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; public class test { public static void main(String[] args) throws Exception { SAXBuilder sa = new SAXBuilder(); Document doc = sa.build(new File("D://test.xml")); Element root = doc.getRootElement(); List list = root.getChildren("disk"); for(int i = 0; i < list.size(); i++) { Element childNode = (Element) list.get(i); String dirveName = childNode.getAttributeValue("name"); String capacity = childNode.getChildText("capacity"); String directories = childNode.getChildText("directories"); String files = childNode.getChildText("files"); System.out.println("磁盘信息:"); System.out.println("分区盘符:"+dirveName); System.out.println("分区容量:"+capacity); System.out.println("目录数:"+directories); System.out.println("文件数:"+files); System.out.println("-----------------------------------"); } } }
输出结果为:
磁盘信息:
分区盘符:C
分区容量:8G
目录数:200
文件数:1580
-----------------------------------
磁盘信息:
分区盘符:D
分区容量:10G
目录数:500
文件数:3000
-----------------------------------