胖娃娃

            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

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


          網站導航:
           
          主站蜘蛛池模板: 奉化市| 邯郸市| 湘乡市| 宁晋县| 乌拉特前旗| 鄂伦春自治旗| 大同市| 富宁县| 四子王旗| 阳新县| 电白县| 玛多县| 遂川县| 南安市| 永济市| 鄱阳县| 濮阳县| 大余县| 克什克腾旗| 临漳县| 吴忠市| 团风县| 嘉祥县| 连南| 靖西县| 邵东县| 尚义县| 凭祥市| 扎兰屯市| 翼城县| 武鸣县| 达日县| 无极县| 正安县| 廊坊市| 朔州市| 镇赉县| 灯塔市| 宝山区| 三明市| 九江市|