<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 495,  comments - 11,  trackbacks - 0

      文檔對象模型 (DOM) 是一個(gè)文檔標(biāo)準(zhǔn),對于完備的文檔和復(fù)雜的應(yīng)用程序,DOM 提供了大量靈活性。DOM標(biāo)準(zhǔn)是標(biāo)準(zhǔn)的。它很強(qiáng)壯且完整,并且有許多實(shí)現(xiàn)。這是許多大型安裝的決定因素--特別是對產(chǎn)品應(yīng)用程序,以避免在API發(fā)生改變時(shí)進(jìn)行大量的改寫。

      以上是我在選擇處理XML數(shù)據(jù)時(shí)之所以沒有選擇JDOM或者dom4j等其它面向?qū)ο蟮臉?biāo)準(zhǔn)的原因,不過也由于DOM從一開始就是一種與語言無關(guān)的模型,而且它更趨向用于像C或Perl這類語言,沒有利用Java的面向?qū)ο蟮男阅埽栽谑褂玫倪^程中也遇到了不少的麻煩,今天這里做一個(gè)小結(jié)。另外,我目前使用XML主要是作為數(shù)據(jù)傳輸?shù)慕y(tǒng)一格式,并統(tǒng)一用戶界面展示的接口,應(yīng)用的面并不是很廣,所以使用到的DOM的內(nèi)容其實(shí)不多。

      在準(zhǔn)備使用它的時(shí)候,是做了充足的準(zhǔn)備的,也有遇到困難的準(zhǔn)備,所以一開始就有了一個(gè)簡單的工具類來封裝DOM對象使用時(shí)必要的公共方法,實(shí)際證明這樣做是很明智的,一個(gè)簡單的創(chuàng)建Document對象的操作,要是每次都需要寫上5行以上代碼,并且還要處理那些煩人的Exception,實(shí)在是會(huì)打擊大家的積極性,所以在最初,做了一個(gè)XMLTool類,專門封裝了如下的公共方法:

      1、 Document對象創(chuàng)建(包括空的Document對象創(chuàng)建,以一個(gè)給定Node節(jié)點(diǎn)作為根節(jié)點(diǎn)創(chuàng)建。

      2、 將一個(gè)規(guī)范的XML字符串轉(zhuǎn)換成一個(gè)Document對象。

      3、 從物理硬盤讀取一個(gè)XML文件并返回一個(gè)Document對象。

      4、 將一個(gè)Node對象轉(zhuǎn)換成字符串。

      其中每個(gè)方法都截獲相關(guān)的DOM操作所拋出的異常,轉(zhuǎn)換成一個(gè)RuntimeException拋出,這些異常在實(shí)際使用過程中,一般狀況下其實(shí)都不會(huì)拋出,特別是象生成一個(gè)Document對象時(shí)的ParserConfigurationException、轉(zhuǎn)換Node節(jié)點(diǎn)成字符串時(shí)要生成一個(gè)Transformer對象時(shí)的TransformerConfigurationException等等,沒有必要在它們身上花時(shí)間精力。而且真就出了相關(guān)的異常的話,其實(shí)根本沒有辦法處理,這樣的狀況通常是系統(tǒng)環(huán)境配置有問題(比如必要的DOM實(shí)現(xiàn)解析器等包沒有加入環(huán)境),所以包裝該異常時(shí)只是很簡要的獲取其Message拋出。

      代碼如下:

    /**
    * 初始化一個(gè)空Document對象返回。
    * @return a Document
    */
    public static Document newXMLDocument() {
     try {
      return newDocumentBuilder().newDocument();
     } catch (ParserConfigurationException e) {
       throw new RuntimeException(e.getMessage());
     }
    }
    /**
    * 初始化一個(gè)DocumentBuilder
    * @return a DocumentBuilder
    * @throws ParserConfigurationException
    */
    public static DocumentBuilder newDocumentBuilder()
    throws ParserConfigurationException {
    return newDocumentBuilderFactory().newDocumentBuilder();
    }
    /**
    * 初始化一個(gè)DocumentBuilderFactory
    * @return a DocumentBuilderFactory
    */
    public static DocumentBuilderFactory newDocumentBuilderFactory() {
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     dbf.setNamespaceAware(true);
     return dbf;
    }
    /**
    * 將傳入的一個(gè)XML String轉(zhuǎn)換成一個(gè)org.w3c.dom.Document對象返回。
    * @param xmlString 一個(gè)符合XML規(guī)范的字符串表達(dá)。
    * @return a Document
    */
    public static Document parseXMLDocument(String xmlString) {
     if (xmlString == null) {
      throw new IllegalArgumentException();
     }
     try {
      return newDocumentBuilder().parse(
       new InputSource(new StringReader(xmlString)));
     } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
     }
    }
    /**
    * 給定一個(gè)輸入流,解析為一個(gè)org.w3c.dom.Document對象返回。
    * @param input
    * @return a org.w3c.dom.Document
    */
    public static Document parseXMLDocument(InputStream input) {
     if (input == null) {
      throw new IllegalArgumentException("參數(shù)為null!");
     }
     try {
      return newDocumentBuilder().parse(input);
     } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
     }
    }
    /**
    * 給定一個(gè)文件名,獲取該文件并解析為一個(gè)org.w3c.dom.Document對象返回。
    * @param fileName 待解析文件的文件名
    * @return a org.w3c.dom.Document
    */
    public static Document loadXMLDocumentFromFile(String fileName) {
     if (fileName == null) {
      throw new IllegalArgumentException("未指定文件名及其物理路徑!");
     }
     try {
      return newDocumentBuilder().parse(new File(fileName));
     } catch (SAXException e) {
      throw new IllegalArgumentException("目標(biāo)文件(" + fileName + ")不能被正確解析為XML!
    " + e.getMessage());
     } catch (IOException e) {
      throw new IllegalArgumentException("不能獲取目標(biāo)文件(" + fileName + ")!
    " + e.getMessage());
     } catch (ParserConfigurationException e) {
      throw new RuntimeException(e.getMessage());
     }
    }
    /**
    * 給定一個(gè)節(jié)點(diǎn),將該節(jié)點(diǎn)加入新構(gòu)造的Document中。
    * @param node a Document node
    * @return a new Document
    */
    public static Document newXMLDocument(Node node) {
     Document doc = newXMLDocument();
     doc.appendChild(doc.importNode(node, true));
     return doc;
    }
    /**
    * 將傳入的一個(gè)DOM Node對象輸出成字符串。如果失敗則返回一個(gè)空字符串""。
    * @param node DOM Node 對象。
    * @return a XML String from node
    */
    public static String toString(Node node) {
     if (node == null) {
      throw new IllegalArgumentException();
     }
     Transformer transformer = newTransformer();
     if (transformer != null) {
      try {
       StringWriter sw = new StringWriter();
       transformer.transform(new DOMSource(node),
        new StreamResult(sw));
        return sw.toString();
      } catch (TransformerException te) {
       throw new RuntimeException(te.getMessage());
      }
     }
     return errXMLString("不能生成XML信息!");
    }
    /**
    * 將傳入的一個(gè)DOM Node對象輸出成字符串。如果失敗則返回一個(gè)空字符串""。
    * @param node DOM Node 對象。
    * @return a XML String from node
    */
    public static String toString(Node node) {
     if (node == null) {
      throw new IllegalArgumentException();
     }
     Transformer transformer = newTransformer();
     if (transformer != null) {
      try {
       StringWriter sw = new StringWriter();
       transformer.transform(new DOMSource(node),new StreamResult(sw));
       return sw.toString();
      } catch (TransformerException te) {
        throw new RuntimeException(te.getMessage());
      }
     }
     return errXMLString("不能生成XML信息!");
    }
    /**
    * 獲取一個(gè)Transformer對象,由于使用時(shí)都做相同的初始化,所以提取出來作為公共方法。
    * @return a Transformer encoding gb2312
    */
    public static Transformer newTransformer() {
     try {
      Transformer transformer =TransformerFactory.newInstance().newTransformer();
      Properties properties = transformer.getOutputProperties();
      properties.setProperty(OutputKeys.ENCODING, "gb2312");
      properties.setProperty(OutputKeys.METHOD, "xml");
      properties.setProperty(OutputKeys.VERSION, "1.0");
      properties.setProperty(OutputKeys.INDENT, "no");
      transformer.setOutputProperties(properties);
      return transformer;
     } catch (TransformerConfigurationException tce) {
      throw new RuntimeException(tce.getMessage());
     }
    }
    /**
    * 返回一段XML表述的錯(cuò)誤信息。提示信息的TITLE為:系統(tǒng)錯(cuò)誤。之所以使用字符串拼裝,主要是這樣做一般
    * 不會(huì)有異常出現(xiàn)。
    * @param errMsg 提示錯(cuò)誤信息
    * @return a XML String show err msg
    */
    public static String errXMLString(String errMsg) {
     StringBuffer msg = new StringBuffer(100);
     msg.append("<?xml version="1.0" encoding="gb2312" ?>");
     msg.append("<errNode title="系統(tǒng)錯(cuò)誤" errMsg="" + errMsg + ""/>");
     return msg.toString();
    }
    /**
    * 返回一段XML表述的錯(cuò)誤信息。提示信息的TITLE為:系統(tǒng)錯(cuò)誤
    * @param errMsg 提示錯(cuò)誤信息
    * @param errClass 拋出該錯(cuò)誤的類,用于提取錯(cuò)誤來源信息。
    * @return a XML String show err msg
    */
    public static String errXMLString(String errMsg, Class errClass) {
     StringBuffer msg = new StringBuffer(100);
     msg.append("<?xml version="1.0" encoding="gb2312" ?>");
     msg.append("<errNode title="系統(tǒng)錯(cuò)誤" errMsg=""+ errMsg
      + "" errSource=""
      + errClass.getName()
      + ""/>");
     return msg.toString();
    }
    /**
    * 返回一段XML表述的錯(cuò)誤信息。
    * @param title 提示的title
    * @param errMsg 提示錯(cuò)誤信息
    * @param errClass 拋出該錯(cuò)誤的類,用于提取錯(cuò)誤來源信息。
    * @return a XML String show err msg
    */
    public static String errXMLString(
     String title,
     String errMsg,
     Class errClass) {
      StringBuffer msg = new StringBuffer(100);
      msg.append("<?xml version="1.0" encoding="gb2312" ?>");
      msg.append("<errNode title=""
       + title
       + "" errMsg=""
       + errMsg
       + "" errSource=""
       + errClass.getName()
       + ""/>");
      return msg.toString();
     }

    posted on 2007-08-20 02:22 jadmin 閱讀(86) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久久精品国产精品亚洲蜜月| 亚洲欧洲日产国码一级毛片| 国内精品99亚洲免费高清| 桃子视频在线观看高清免费视频| 毛片a级毛片免费观看免下载| 亚洲色欲久久久久综合网| 国产亚洲漂亮白嫩美女在线| 无码不卡亚洲成?人片| 四虎一区二区成人免费影院网址| 波多野结衣免费在线| 亚洲人成77777在线播放网站| 亚洲va久久久久| 很黄很污的网站免费| 亚洲精品高清无码视频| 青青草原1769久久免费播放| 四虎永久在线精品免费影视| 麻豆亚洲AV成人无码久久精品 | 亚洲精品视频在线看| 一本大道一卡二大卡三卡免费| 日本一区二区三区免费高清| 亚洲中文字幕一区精品自拍| 国产免费黄色大片| 国产精品免费看久久久香蕉| 国产A在亚洲线播放| 久草视频在线免费| 国产精品成人亚洲| 亚洲精品国产精品乱码不卡√| 99国产精品永久免费视频| 亚洲风情亚Aⅴ在线发布| 国产精品亚洲w码日韩中文| 午夜老司机永久免费看片| 精品国产成人亚洲午夜福利| jizzjizz亚洲| 亚洲成人免费网址| 国产精品久久亚洲一区二区| 久久国产亚洲精品麻豆| 成年女人毛片免费播放视频m| 久久最新免费视频| 亚洲 欧洲 视频 伦小说| 亚洲日本中文字幕一区二区三区| 91视频免费网址|