import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileReader;import java.math.BigDecimal;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/** *//** * <b>功能描述:解析xml文件</b><br/> * <p> * 创建时间: 2007-07-01 <br/> * * @version 1.00 * @author yefeng*/public class TestXML ...{ private void getXMLFile(String fileName) throws Exception ...{ //创建 DocumentBuilderFactory。 DocumentBuilderFactory 对象创建 DocumentBuilder。 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //创建 DocumentBuilder。 DocumentBuilder 执行实际的解析以创建 Document 对象。 DocumentBuilder builder = factory.newDocumentBuilder(); //解析文件以创建 Document 对象。 Document doc = builder.parse(new File(fileName)); Node rootNode = getChildNodeByName(doc, "departFile"); Node summaryNode = getChildNodeByName(rootNode, "TotalField"); // 解析xml文件 NamedNodeMap attr = summaryNode.getAttributes(); String SumDepart = attr.getNamedItem("SumDepart").getNodeValue(); String TotalCount = attr.getNamedItem("TotalCount").getNodeValue(); printXML(SumDepart, TotalCount); } private Node getChildNodeByName(Node parent, String name) throws Exception ...{ NodeList nList = parent.getChildNodes(); for (int i = 0; i < nList.getLength(); i++) ...{ Node tmp = nList.item(i); if (name.equals(tmp.getNodeName())) ...{ return tmp; } } return null; } private void getDetailXML(String XMLFile) throws Exception ...{ BufferedReader in = null; String aFileName = XMLFile; try ...{ in = new BufferedReader(new FileReader(aFileName)); String line; // 解析批量文件中的有效数据(文件中包含的每笔指令) while (true) ...{ line = in.readLine(); if (line == null) break; if (line.trim().length() == 0) continue; // 表示文件结束 if (line.trim().indexOf("</departFile") != -1) break; // 过滤无效行 if (line.trim().length() == 0 || line.indexOf("<!") != -1 || line.indexOf("<Record ") == -1) continue; // 用xml来解析明细 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); ByteArrayInputStream stream = new ByteArrayInputStream(line .getBytes("UTF-8")); Document doc = builder.parse(stream); Node record = doc.getFirstChild(); NamedNodeMap attr = record.getAttributes(); for (int i = 0; i < attr.getLength(); i++) ...{ String name = attr.item(i).getNodeName(); String value = attr.item(i).getNodeValue(); //打印信息 System.out.print(value+" "); } System.out.println(""); } } catch (Exception e) ...{ System.out.println(e.getMessage()); } finally ...{ if (in != null) ...{ in.close(); } } } private void printXML(String str1, String str2) ...{//打印概要信息 System.out.println("SumDepart: " + str1); System.out.println("TotalCount: " + str2); } public static void main(String[] str) ...{ TestXML xmlObj = new TestXML(); String fileName = "XMLFile.xml"; try ...{ xmlObj.getXMLFile(fileName); System.out.println("departName"+" "+"departNo"+" "+"count"+" "+"remark"); xmlObj.getDetailXML(fileName); } catch (Exception e) ...{ System.out.println(e.getMessage()); } }} /*xml格式*/<?xml version="1.0" encoding="GB2312"?><departFile> <TotalField SumDepart="3" TotalCount="300"/> <!--#部门名称|部门id|部门人数|摘要|--> <RecordList> <Record departName="depart1" departNo="1" count="120" remark="sd"/> <Record departName="depart2" departNo="2" count="100" remark="dd"/> <Record departName="depart3" departNo="3" count="80" remark="aaaa"/> </RecordList></departFile>