dwys0343

          技術(shù)整理

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

          其中每個方法都截獲相關(guān)的dom操作所拋出的異常,轉(zhuǎn)換成一個runtimeexception拋出,這些異常在實際使用過程中,一般狀況下其實都不會拋出,特別是象生成一個document對象時的parserconfigurationexception、轉(zhuǎn)換node節(jié)點成字符串時要生成一個transformer對象時的transformerconfigurationexception等等,沒有必要在它們身上花時間精力。而且真就出了相關(guān)的異常的話,其實根本沒有辦法處理,這樣的狀況通常是系統(tǒng)環(huán)境配置有問題(比如必要的dom實現(xiàn)解析器等包沒有加入環(huán)境),所以包裝該異常時只是很簡要的獲取其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轉(zhuǎn)換成一個org.w3c.dom.document對象返回。
          * @param xmlstring 一個符合xml規(guī)范的字符串表達。
          * @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("參數(shù)為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(
          "目標(biāo)文件(" + filename + ")不能被正確解析為xml!\n" + e.getmessage());
          } catch (ioexception e) {
          throw new illegalargumentexception(
          "不能獲取目標(biāo)文件(" + filename + ")!\n" + e.getmessage());
          } catch (parserconfigurationexception e) {
          throw new runtimeexception(e.getmessage());
          }
          }
          /**
          * 給定一個節(jié)點,將該節(jié)點加入新構(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;
          }

          /**
          * 將傳入的一個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為:系統(tǒng)錯誤。之所以使用字符串拼裝,主要是這樣做一般
          * 不會有異常出現(xiàn)。
          * @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=\"系統(tǒng)錯誤\" errmsg=\"" + errmsg + "\"/>");
          return msg.tostring();
          }
          /**
          * 返回一段xml表述的錯誤信息。提示信息的title為:系統(tǒng)錯誤
          * @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=\"系統(tǒng)錯誤\" 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的基本應(yīng)用,所以就不一一詳細說明了。
          在實際使用過程中,有幾種狀況使用很頻繁,但是dom的接口的設(shè)計卻使該操作很麻煩,所以分別添加了相應(yīng)的處理方法。
          其中最麻煩的要數(shù)獲取一個節(jié)點的text子節(jié)點文本信息了,如下的xml節(jié)點:
          <element>
          text
          </element>
          在擁有element節(jié)點對象時,要獲取其中的文本信息"text",首先要獲取element節(jié)點的子節(jié)點列表,要判斷其是否存在子節(jié)點,如果存在,那么遍歷其子節(jié)點找到一個textnode節(jié)點,通過getnodevalue()方法來獲取該文本信息,由于這里element節(jié)點沒有信息時沒有子節(jié)點,所以必須判斷element節(jié)點是否存在子節(jié)點才能去訪問真正包含了文本信息的textnode節(jié)點,那么如果要處理的數(shù)據(jù)都是以這種形式給出的,就會增加大量的開發(fā)代碼同時讓開發(fā)工作枯燥無味,因此這里使用了一個默認的約定實現(xiàn),就是,給出了一個公共方法,該方法取給定node下的直接子節(jié)點的text節(jié)點文本信息,如果不存在text節(jié)點則返回null,這個約定雖然使該方法的使用有所限制,也可能導(dǎo)致錯誤使用該方法,但是,按實際使用的狀況來看,這樣的約定和使用方式是沒有問題的,因為實際用到的都是上面舉的例子的狀況,代碼:
          /**
          * 這個方法獲取給定node下的text節(jié)點文本信息,如果不存在text節(jié)點則返回null。
          * 注意:是直接子節(jié)點,相差2層或2層以上不會被考慮。
          * @param node a node 一個node。
          * @return a string 如果給定節(jié)點存在text子節(jié)點,則返回第一個訪問到的text子節(jié)點文本信息,如果不存在則返回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節(jié)點,如果不存在text節(jié)點則返回null。
          * 注意:是直接子節(jié)點,相差2層或2層以上不會被考慮。
          * @param node a node 一個node。
          * @return a text 如果給定節(jié)點存在text子節(jié)點,則返回第一個訪問到的text子節(jié)點,如果不存在則返回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節(jié)點的直接text子節(jié)點分開包裝。

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

          還有一種,是由于dom2的限制導(dǎo)致的,dom2規(guī)范中,不能將一個document doca的節(jié)點直接加入到另一個document docb對象的節(jié)點的子節(jié)點列表中,要這么做必須首先將doca的節(jié)點通過docb的importnode方法轉(zhuǎn)換后在添加到目標(biāo)節(jié)點的子節(jié)點列表中,所以也有一個方法來統(tǒng)一處理:
          /**
          * 這個方法將參數(shù)appendeddoc的根節(jié)點及其以下節(jié)點附加到doc的跟節(jié)點下面。
          * 作為doc的跟節(jié)點的作后一個子節(jié)點。
          * 相當(dāng)于: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));
          }
          }
          /**
          * 這個方法將參數(shù)appendeddoc的根節(jié)點及其以下節(jié)點附加到node節(jié)點下面。
          * 作為node節(jié)點的作后一個子節(jié)點。
          * 相當(dāng)于:node.appenddoc(appendednode);
          * @param node 待添加的節(jié)點將被添加到該節(jié)點的最后。
          * @param appendednode a node 這個節(jié)點將被添加作為node節(jié)點的最后一個子節(jié)點。
          */
          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
          主站蜘蛛池模板: 永清县| 汝城县| 弥渡县| 阜南县| 鹤壁市| 慈利县| 禄劝| 鲁山县| 措勤县| 台北县| 米易县| 澄城县| 延长县| 云南省| 石城县| 信丰县| 闽侯县| 龙泉市| 含山县| 宁河县| 昌江| 兴义市| 惠安县| 界首市| 博湖县| 美姑县| 肇源县| 当雄县| 巴东县| 土默特左旗| 佛坪县| 丰城市| 开远市| 吉水县| 江油市| 米林县| 合水县| 基隆市| 乌鲁木齐县| 盘锦市| 高州市|