胖娃娃

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            4 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks

            昨天學習了對XML的讀操作,今天嘗試修改XML中的內容,具體代碼如下(SCXMLUtils.java):

          package ?SCXML;

          import ?java.io.FileWriter;

          import ?javax.xml.parsers.DocumentBuilder;
          import ?javax.xml.parsers.DocumentBuilderFactory;
          import ?javax.xml.transform.Transformer;
          import ?javax.xml.transform.TransformerFactory;
          import ?javax.xml.transform.dom.DOMSource;
          import ?javax.xml.transform.stream.StreamResult;

          import ?org.w3c.dom.Document;
          import ?org.w3c.dom.Node;
          import ?org.w3c.dom.NodeList;

          public ? class ?SCXMLUtils? {

          ????
          private ?SCXMLUtils()? {
          ????}


          ????
          public ? static ?Document?getXMLDoc(String?xmlfile)? {
          ????????
          try ? {

          ????????????DocumentBuilderFactory?factory?
          = ?DocumentBuilderFactory
          ????????????????????.newInstance();
          ????????????DocumentBuilder?builder?
          = ?factory.newDocumentBuilder();
          ????????????Document?doc?
          = ?builder.parse(xmlfile);
          ????????????
          return ?doc;

          ????????}
          ? catch ?(Exception?e)? {

          ????????????System.out.println(
          " an?error?occurred?in?SCXMLUtils: " ? + ?e);
          ????????????
          return ? null ;

          ????????}


          ????}


          ????
          public ? static ?Node?getChild(Node?parent,?String?name)? {

          ????????NodeList?children?
          = ?parent.getChildNodes();
          ????????
          int ?len? = ?children.getLength();
          ????????Node?node?
          = ? null ;
          ????????
          for ?( int ?i? = ? 0 ;?i? < ?len;?i ++ )? {

          ????????????node?
          = ?children.item(i);
          ????????????String?thisname?
          = ?node.getNodeName();
          ????????????
          if ?(name.equals(thisname))? {

          ????????????????
          return ?node;

          ????????????}


          ????????}


          ????????
          return ? null ;
          ????}


          ????
          public ? static ?String?getTextValue(Node?node)? {

          ????????
          if ?(???(node.getNodeType()? == ?Node.TEXT_NODE)
          ????????????
          || ?(node.getNodeType()? == ?Node.CDATA_SECTION_NODE))? {

          ????????????
          return ?node.getNodeValue();

          ????????}
          ? else ? {

          ????????????
          return ? null ;

          ????????}


          ????}


          ????
          public ? static ? void ?setTextValue(Document?xmldoc,?Node?node,?String?value)? {

          ????????
          if ?(???(node.getNodeType()? == ?Node.TEXT_NODE)
          ????????????
          || ?(node.getNodeType()? == ?Node.CDATA_SECTION_NODE))? {

          ????????????
          try ? {
          ????????????????TransformerFactory?tf?
          = ?TransformerFactory.newInstance();
          ????????????????Transformer?tr?
          = ?tf.newTransformer();
          ????????????????tr.setOutputProperty(
          " encoding " ,? " GBK " );
          ????????????????node.setNodeValue(value);
          ????????????????DOMSource?ds?
          = ? new ?DOMSource(xmldoc);
          ????????????????StreamResult?sr?
          = ? new ?StreamResult( new ?FileWriter( " test.xml " ));
          ????????????????tr.transform(ds,?sr);
          ????????????}
          ? catch ?(Exception?e)? {
          ????????????????
          ????????????????System.out.println(
          " SCXMLUtils:? " ? + ?e);

          ????????????}

          ????????}
          ? else ? {

          ????????????System.out.println(
          " SCXMLUtils:?This?node?is?not?a?text! " );

          ????????}


          ????}


          }


          說明:最關鍵的變化就是增加了setTextValue這一個函數,其中tr.setOutputProperty("encoding", "GBK")很重要,否則寫入中文會出現亂碼,?
             node.setNodeValue(value)這句只是修改內存中XML節點的值,所以還要建立DOMSource對象和StreamResult對象,將內存中的XML
             信息寫回文件。

          TestSCXML.java基本沒有什么變化,就在最后增加了測試setTextValue的代碼:

          SCXMLUtils.setTextValue(xmldoc,?node1.getFirstChild(),?"你好");

          現在運行TestSCXML看看XML是否被修改了,

          posted on 2006-06-19 22:55 胖娃娃 閱讀(443) 評論(0)  編輯  收藏 所屬分類: XML

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


          網站導航:
           
          主站蜘蛛池模板: 蚌埠市| 沾益县| 翁牛特旗| 南川市| 庆元县| 哈巴河县| 增城市| 台南县| 汶上县| 湟源县| 锡林郭勒盟| 陵川县| 太仆寺旗| 武威市| 涪陵区| 通辽市| 富锦市| 磴口县| 穆棱市| 巫山县| 兰西县| 蓝田县| 古蔺县| 巨鹿县| 玉林市| 金湖县| 澎湖县| 永定县| 新闻| 黄大仙区| 北辰区| 缙云县| 马龙县| 高密市| 临洮县| 慈溪市| 渭源县| 木里| 科技| 阳新县| 钦州市|