dwys0343

          技術整理

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            1 Posts :: 16 Stories :: 0 Comments :: 0 Trackbacks
          文檔對象模型 (dom) 是一個文檔標準,對于完備的文檔和復雜的應用程序,dom 提供了大量靈活性。dom標準是標準的。它很強壯且完整,并且有許多實現。這是許多大型安裝的決定因素--特別是對產品應用程序,以避免在api發生改變時進行大量的改寫。
          以上是我在選擇處理xml數據時之所以沒有選擇jdom或者dom4j等其它面向對象的標準的原因,不過也由于dom從一開始就是一種與語言無關的模型,而且它更趨向用于像c或perl這類語言,沒有利用java的面向對象的性能,所以在使用的過程中也遇到了不少的麻煩,今天這里做一個小結。另外,我目前使用xml主要是作為數據傳輸的統一格式,并統一用戶界面展示的接口,應用的面并不是很廣,所以使用到的dom的內容其實不多。
          在準備使用它的時候,是做了充足的準備的,也有遇到困難的準備,所以一開始就有了一個簡單的工具類來封裝dom對象使用時必要的公共方法,實際證明這樣做是很明智的,一個簡單的創建document對象的操作,要是每次都需要寫上5行以上代碼,并且還要處理那些煩人的exception,實在是會打擊大家的積極性,所以在最初,做了一個xmltool類,專門封裝了如下的公共方法:
          1、 document對象創建(包括空的document對象創建,以一個給定node節點作為根節點創建。
          2、 將一個規范的xml字符串轉換成一個document對象。
          3、 從物理硬盤讀取一個xml文件并返回一個document對象。
          4、 將一個node對象轉換成字符串。

          其中每個方法都截獲相關的dom操作所拋出的異常,轉換成一個runtimeexception拋出,這些異常在實際使用過程中,一般狀況下其實都不會拋出,特別是象生成一個document對象時的parserconfigurationexception、轉換node節點成字符串時要生成一個transformer對象時的transformerconfigurationexception等等,沒有必要在它們身上花時間精力。而且真就出了相關的異常的話,其實根本沒有辦法處理,這樣的狀況通常是系統環境配置有問題(比如必要的dom實現解析器等包沒有加入環境),所以包裝該異常時只是很簡要的獲取其message拋出。
          代碼如下:
          /**
          * 初始化一個空document對象返回。
          * @return a document
          */
          public static document newxmldocument() {
          try {
          return newdocumentbuilder().newdocument();
          } catch (parserconfigurationexception e) {
          throw new runtimeexception(e.getmessage());
          }
          }

          /**
          * 初始化一個documentbuilder
          * @return a documentbuilder
          * @throws parserconfigurationexception
          */
          public static documentbuilder newdocumentbuilder()
          throws parserconfigurationexception {
          return newdocumentbuilderfactory().newdocumentbuilder();
          }

          /**
          * 初始化一個documentbuilderfactory
          * @return a documentbuilderfactory
          */
          public static documentbuilderfactory newdocumentbuilderfactory() {
          documentbuilderfactory dbf = documentbuilderfactory.newinstance();
          dbf.setnamespaceaware(true);
          return dbf;
          }
          /**
          * 將傳入的一個xml string轉換成一個org.w3c.dom.document對象返回。
          * @param xmlstring 一個符合xml規范的字符串表達。
          * @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());
          }
          }

          /**
          * 給定一個輸入流,解析為一個org.w3c.dom.document對象返回。
          * @param input
          * @return a org.w3c.dom.document
          */
          public static document parsexmldocument(inputstream input) {
          if (input == null) {
          throw new illegalargumentexception("參數為null!");
          }
          try {
          return newdocumentbuilder().parse(input);
          } catch (exception e) {
          throw new runtimeexception(e.getmessage());
          }
          }
          /**
          * 給定一個文件名,獲取該文件并解析為一個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(
          "目標文件(" + filename + ")不能被正確解析為xml!\n" + e.getmessage());
          } catch (ioexception e) {
          throw new illegalargumentexception(
          "不能獲取目標文件(" + filename + ")!\n" + e.getmessage());
          } catch (parserconfigurationexception e) {
          throw new runtimeexception(e.getmessage());
          }
          }
          /**
          * 給定一個節點,將該節點加入新構造的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;
          }

          /**
          * 將傳入的一個dom node對象輸出成字符串。如果失敗則返回一個空字符串""。
          * @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信息!");
          }
          /**
          * 將傳入的一個dom node對象輸出成字符串。如果失敗則返回一個空字符串""。
          * @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信息!");
          }
          /**
          * 獲取一個transformer對象,由于使用時都做相同的初始化,所以提取出來作為公共方法。
          * @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表述的錯誤信息。提示信息的title為:系統錯誤。之所以使用字符串拼裝,主要是這樣做一般
          * 不會有異常出現。
          * @param errmsg 提示錯誤信息
          * @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=\"系統錯誤\" errmsg=\"" + errmsg + "\"/>");
          return msg.tostring();
          }
          /**
          * 返回一段xml表述的錯誤信息。提示信息的title為:系統錯誤
          * @param errmsg 提示錯誤信息
          * @param errclass 拋出該錯誤的類,用于提取錯誤來源信息。
          * @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=\"系統錯誤\" errmsg=\""
          + errmsg
          + "\" errsource=\""
          + errclass.getname()
          + "\"/>");
          return msg.tostring();
          }
          /**
          * 返回一段xml表述的錯誤信息。
          * @param title 提示的title
          * @param errmsg 提示錯誤信息
          * @param errclass 拋出該錯誤的類,用于提取錯誤來源信息。
          * @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();
          }

          以上都是dom的基本應用,所以就不一一詳細說明了。
          在實際使用過程中,有幾種狀況使用很頻繁,但是dom的接口的設計卻使該操作很麻煩,所以分別添加了相應的處理方法。
          其中最麻煩的要數獲取一個節點的text子節點文本信息了,如下的xml節點:
          <element>
          text
          </element>
          在擁有element節點對象時,要獲取其中的文本信息"text",首先要獲取element節點的子節點列表,要判斷其是否存在子節點,如果存在,那么遍歷其子節點找到一個textnode節點,通過getnodevalue()方法來獲取該文本信息,由于這里element節點沒有信息時沒有子節點,所以必須判斷element節點是否存在子節點才能去訪問真正包含了文本信息的textnode節點,那么如果要處理的數據都是以這種形式給出的,就會增加大量的開發代碼同時讓開發工作枯燥無味,因此這里使用了一個默認的約定實現,就是,給出了一個公共方法,該方法取給定node下的直接子節點的text節點文本信息,如果不存在text節點則返回null,這個約定雖然使該方法的使用有所限制,也可能導致錯誤使用該方法,但是,按實際使用的狀況來看,這樣的約定和使用方式是沒有問題的,因為實際用到的都是上面舉的例子的狀況,代碼:
          /**
          * 這個方法獲取給定node下的text節點文本信息,如果不存在text節點則返回null。
          * 注意:是直接子節點,相差2層或2層以上不會被考慮。
          * @param node a node 一個node。
          * @return a string 如果給定節點存在text子節點,則返回第一個訪問到的text子節點文本信息,如果不存在則返回null。
          */
          public static string getnodevalue(node node) {
          if (node == null) {
          return null;
          }

          text text = gettextnode(node);

          if (text != null) {
          return text.getnodevalue();
          }

          return null;
          }

          /**
          * 這個方法獲取給定node下的text節點,如果不存在text節點則返回null。
          * 注意:是直接子節點,相差2層或2層以上不會被考慮。
          * @param node a node 一個node。
          * @return a text 如果給定節點存在text子節點,則返回第一個訪問到的text子節點,如果不存在則返回null。
          */
          public static text gettextnode(node node) {
          if (node == null) {
          return null;
          }
          if (node.haschildnodes()) {
          nodelist list = node.getchildnodes();
          for (int i = 0; i < list.getlength(); i++) {
          if (list.item(i).getnodetype() == node.text_node) {
          return (text) list.item(i);
          }
          }
          }
          return null;
          }

          上面代碼將獲取給定node節點的直接text子節點分開包裝。

          另一個很經常碰到的狀況是,我希望直接定位到目標節點,獲取該節點對象,而不需要通過一層一層的節點遍歷來找到目標節點,dom2接口中至少提供了如下的方式來定位節點:
          1、 對于document對象:
          1) getdocumentelement()――獲取根節點對象,實際很少使用的,因為根節點基本也就只是根節點而已,實際的數據節點都是根節點下的直接子節點開始的。
          2) getelementbyid(string elementid)――這個方法本來應該是一個最佳的定位方法,但是在實際使用過程中沒有被我使用,其主要原因就是,這里的"id"不同于一個節點的屬性"id",這在org.w3c.dom.document的api說明中是明確指出,而我找了不少的資料也沒有看到有關的使用方式,所以只好放棄了。
          3) getelementsbytagname(string tagname)――這個方法其實是沒有辦法的選擇,只好用它了,不過實際倒也很合用,雖然該方法返回的是一個nodelist,但是實際使用時,將節點的tagname設計成特殊字符串,那么就可以直接獲取了,而實際使用時,其實也差不多,很多時候會直接拿數據庫中的字段名來作為tagname,以方便得獲取該字段得值,在一個簡單得約定下,使用了如下方法:
          /**
          * 這個方法檢索參數element下所有tagname為:tagname的節點,并返回節點列表的第一個節點。
          * 如果不存在該tagname的節點,則返回null。
          * @param element 待搜索節點
          * @param tagname 待搜索標簽名
          * @return a element 獲得以tagname為標簽名的節點列表的第一個節點。
          */
          public static element getfirstelementbyname(
          element element,
          string tagname) {
          return (element) getfirstelement(element.getelementsbytagname(tagname));
          }
          /**
          * 從給定節點列表中獲取第一個節點返回,如果節點集合為null/空,則返回null。
          * @param nodelist a nodelist
          * @return a node
          */
          private static node getfirstelement(nodelist nodelist) {
          if (nodelist == null || nodelist.getlength() == 0) {
          return null;
          }
          return nodelist.item(0);
          }
          這個約定看似限制很大,其實實際使用時基本都是這樣的,只要獲取第一個給定tagname的element節點就可以了的。
          4)getelementsbytagnamens(string namespaceuri, string localname)――這個方法基本沒有使用,因為還沒有碰到需要使用命名空間的狀況。
          2、 對于element對象――――element對象和document對象雷同,少了getdocumentelement()方法,不過和document一樣也都是主要使用getelementsbytagname()方法。
          3、 其它的節點對象基本沒有直接定位的訪問方法

          還有一種,是由于dom2的限制導致的,dom2規范中,不能將一個document doca的節點直接加入到另一個document docb對象的節點的子節點列表中,要這么做必須首先將doca的節點通過docb的importnode方法轉換后在添加到目標節點的子節點列表中,所以也有一個方法來統一處理:
          /**
          * 這個方法將參數appendeddoc的根節點及其以下節點附加到doc的跟節點下面。
          * 作為doc的跟節點的作后一個子節點。
          * 相當于:doc.appenddoc(appendeddoc);
          * @param doc a document
          * @param appendeddoc a document
          */
          public static void appendxmldocument(document doc, document appendeddoc) {
          if (appendeddoc != null) {
          doc.getfirstchild().appendchild(
          doc.importnode(appendeddoc.getfirstchild(), true));
          }
          }
          /**
          * 這個方法將參數appendeddoc的根節點及其以下節點附加到node節點下面。
          * 作為node節點的作后一個子節點。
          * 相當于:node.appenddoc(appendednode);
          * @param node 待添加的節點將被添加到該節點的最后。
          * @param appendednode a node 這個節點將被添加作為node節點的最后一個子節點。
          */
          public static void appendxmldocument(node node, node appendednode) {
          if (appendednode == null) {
          return;
          }
          if (appendednode instanceof document) {
          appendednode = ((document) appendednode).getdocumentelement();
          }
          node.appendchild(
          node.getownerdocument().importnode(appendednode, true));
          }

          基本上就這些常用的了,其它還有一些零碎的方法,不過都不常用到,就不作介紹了。另外要說的是,如果哪位知道上面說到的getelementbyid()方法的具體可行的方便用法,也請指教下。
          posted on 2007-03-07 12:46 特蘭克斯 閱讀(189) 評論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 枣阳市| 天津市| 天峻县| 平昌县| 西昌市| 合山市| 灯塔市| 安康市| 锡林浩特市| 库伦旗| 周宁县| 天镇县| 东阳市| 枣庄市| 渑池县| 昭平县| 财经| 措勤县| 纳雍县| 永仁县| 织金县| 新密市| 仙桃市| 松阳县| 克什克腾旗| 济宁市| 富阳市| 辛集市| 宜城市| 鱼台县| 延安市| 芜湖县| 扶绥县| 廉江市| 莒南县| 余姚市| 观塘区| 九龙城区| 荥阳市| 云阳县| 城固县|