- /**************************************
- * Title: 读取XML配置文件
- * Description: DOM解析基础类
- * date : 2006-3-22
- * author : zhanglei
- ***************************************/
- package server;
- import java.io.*; //Java基础包,包含各种IO操作
- import java.util.*; //Java基础包,包含各种标准数据结构操作
- import javax.xml.parsers.*; //XML解析器接口
- import org.w3c.dom.*; //XML的DOM实现
- //import javax.xml.transform.*;
- //import javax.xml.transform.dom.DOMSource;
- //import javax.xml.transform.stream.StreamResult;
- //import java.sql.*;
- public class DomXML {
- private Document doc;
- private static Element root;
- public Vector item_Vector;
- private static Element begingItem;
- private NodeList listMy;
- public DomXML(String inFile) throws Exception {
- System.out.println("初始化Dom:" + inFile);
- //为解析XML作准备,创建DocumentBuilderFactory实例,指定DocumentBuilder
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true); //new add
- DocumentBuilder db = null;
- try {
- db = dbf.newDocumentBuilder();
- }
- catch (ParserConfigurationException pce) {
- SaveLog.SetInfo("DomXML1:"+pce, 2);//出异常时输出异常信息,然后退出,下同
- }
- doc = null;
- try {
- doc = db.parse(new File(inFile));
- doc.getDocumentElement().normalize(); //new add
- }
- catch (DOMException dom) {
- SaveLog.SetInfo("DomXML2:" + dom.getMessage(), 2);
- }
- catch (IOException ioe) {
- SaveLog.SetInfo("DomXML3:" + ioe, 2);
- }
- //解析XML全过程,先取根元素
- root = doc.getDocumentElement();
- }
- public static Element GetBeginItem(String beginItemName) {
- //设置开始节点位置
- try {
- NodeList items = root.getElementsByTagName(beginItemName);
- begingItem = (Element) items.item(0);
- return begingItem;
- }
- catch (Exception ex) {
- SaveLog.SetInfo("DomXML中GetBeginItem:" + ex, 2);
- return null;
- }
- }
- public int GetItemCount(String beginItemName) {
- //设置开始节点下的子接点数
- try {
- NodeList items = root.getElementsByTagName(beginItemName);
- begingItem = (Element) items.item(0);
- Node nodeitm = items.item(0);
- if (nodeitm.hasChildNodes()) {
- listMy = nodeitm.getChildNodes();
- return listMy.getLength();
- }
- return 0;
- }
- catch (Exception ex) {
- SaveLog.SetInfo("DomXML中GetItemCount:" + ex, 2);
- return 0;
- }
- }
- public String GetItemNoName(int itemNo) {
- //获取开始节点下的子接点数的名
- try {
- Node nodea = listMy.item(itemNo);
- if (nodea.getNodeName().indexOf("#text") < 0 && nodea.getNodeName() != null) {
- return nodea.getNodeName() ;
- }
- return "";
- }
- catch (Exception ex) {
- SaveLog.SetInfo("DomXML中GetItemNoName:" + ex, 2);
- return "";
- }
- }
- public String GetItemNoVal(int itemNo) {
- //设置开始节点下的子接点数的值
- try {
- Node nodea = listMy.item(itemNo);
- if (nodea.getNodeName().indexOf("#text") < 0) {
- return nodea.getNodeValue() ;
- }
- return "";
- }
- catch (Exception ex) {
- SaveLog.SetInfo("DomXML中GetItemNoVal:" + ex, 2);
- return "";
- }
- }
- public String GetItemNoAttribute(int itemNo, String sxName) {
- //设置开始节点下的子接点数的属性值
- try {
- Element nodea = (Element)listMy.item(itemNo);
- return nodea.getAttribute(sxName);
- }
- catch (Exception ex) {
- SaveLog.SetInfo("DomXML中GetItemNoAttribute:" + ex, 2);
- return "";
- }
- }
- public static String GetItemVal(String itemName) {
- //取节点的值
- try {
- NodeList names = begingItem.getElementsByTagName(itemName);
- if (names.getLength() == 1) {
- Element e = (Element) names.item(0);
- Text t = (Text) e.getFirstChild();
- return t.getNodeValue();
- }
- return "";
- }
- catch (Exception ex) {
- // SaveLog.SetInfo("DomXML中GetItemVal:" + ex, 2);
- return "";
- }
- }
- //从itemName元素取得属性名为sxName的属性值
- public static String GetItemAttribute(String itemName, String sxName) {
- //取节点的属性
- try {
- NodeList names = begingItem.getElementsByTagName(itemName);
- if (names.getLength() == 1) {
- Element e = (Element) names.item(0);
- return e.getAttribute(sxName);
- }
- return "";
- }
- catch (Exception ex) {
- SaveLog.SetInfo("DomXML中GetItemAttribute:" + ex, 2);
- return "";
- }
- }
- public static void main(String[] args) throws Exception {
- //建立测试实例
- String f = "config.xml";
- System.out.println(f);
- DomXML SysXml = new DomXML(f);
- for (int m = 0; m < SysXml.GetItemCount("T_JHXX_GAXXB"); m++) {
- System.out.println(SysXml.GetItemNoName(m));
- } //初始化向量列表
- DomXML linkXml = new DomXML("links.xml");
- for (int m = 0; m < linkXml.GetItemCount("link1"); m++) {
- System.out.println(linkXml.GetItemNoVal(m));
- System.out.println(linkXml.GetItemNoName(m));
- } //初始化向量列表
- // domxml.item_Vector = new Vector();
- DomXML DomXML = new DomXML(f);
- // DomXML.GetBeginItem("CheckXmlConfig");
- // System.out.println(SysXml.GetItemVal("CheckAll"));
- // domxml.GetBeginItem("data");
- // DomXML.GetBeginItem("T_JHXX_GAXXB");
- System.out.println(SysXml.GetItemAttribute("CZRKGMSFHM", "itemtype"));
- System.out.println(DomXML.GetItemCount("T_JHXX_GAXXB"));
- }
- }