隨筆-204  評論-90  文章-8  trackbacks-0
          代碼如下:
          package?com.willpower.parsexml;

          import?java.io.File;
          import?java.io.FileWriter;
          import?java.io.IOException;
          import?java.util.Iterator;
          import?java.util.List;

          import?org.dom4j.Attribute;
          import?org.dom4j.Document;
          import?org.dom4j.DocumentException;
          import?org.dom4j.DocumentHelper;
          import?org.dom4j.Element;
          import?org.dom4j.io.OutputFormat;
          import?org.dom4j.io.SAXReader;
          import?org.dom4j.io.XMLWriter;

          public?class?ParseXml?{

          ????
          /**
          ?????*?創建XML文件
          ?????*?
          @author?cnyqiao@hotmail.com
          ?????*?
          @param?fileName
          ?????*?@date?Jul?10,?2006?3:34:58?PM
          ?????*?
          @return?rtn?true?or?false
          ?????
          */

          ????
          public?boolean?createXMLFile(String?fileName)?{
          ????????
          boolean?rtn?=?false;
          ????????
          //?使用DocumentHelper.createDocument方法建立一個文檔實例
          ????????Document?document?=?DocumentHelper.createDocument();
          ????????
          //?使用addElement方法方法創建根元素
          ????????Element?catalogElement?=?document.addElement("catalog");
          ????????
          //?使用addComment方法方法向catalog元素添加注釋
          ????????catalogElement.addComment("An?XML?cataog");
          ????????
          //?使用addProcessInstruction向catalog元素增加處理指令
          ????????catalogElement.addProcessingInstruction("target",?"text");

          ????????
          //?使用addElement方法向catalog元素添加journal子元素
          ????????Element?journalElement?=?catalogElement.addElement("journal");
          ????????
          //?使用addAttribute方法向journal元素添加title和publisher屬性
          ????????journalElement.addAttribute("title",?"XML?Zone");
          ????????journalElement.addAttribute(
          "publisher",?"Willpower?Co");

          ????????
          //?使用addElement方法向journal元素添加article子元素
          ????????Element?articleElement?=?journalElement.addElement("article");
          ????????
          //?使用addAttribute方法向article元素添加level和date屬性
          ????????articleElement.addAttribute("level",?"Intermediate");
          ????????articleElement.addAttribute(
          "date",?"July-2006");

          ????????
          //?使用addElement方法向article元素添加title子元素
          ????????Element?titleElement?=?articleElement.addElement("title");
          ????????
          //?使用setText方法設置title子元素的值
          ????????titleElement.setText("Dom4j?Create?XML?Schema");

          ????????
          //?使用addElement方法向article元素添加authorElement子元素
          ????????Element?authorElement?=?articleElement.addElement("author");

          ????????
          //?使用addElement方法向author元素添加firstName子元素
          ????????Element?firstName?=?authorElement.addElement("fistname");
          ????????
          //?使用setText方法設置firstName子元素的值
          ????????firstName.setText("Yi");

          ????????
          //?使用addElement方法向author元素添加lastname子元素
          ????????Element?lastName?=?authorElement.addElement("lastname");
          ????????
          //?使用setText方法設置lastName子元素的值
          ????????lastName.setText("Qiao");

          ????????XMLWriter?output;
          ????????
          //輸出格式化
          ????????OutputFormat?format?=?OutputFormat.createPrettyPrint();
          ????????
          try?{
          ????????????output?
          =?new?XMLWriter(new?FileWriter(fileName),?format);
          ????????????output.write(document);
          ????????????output.close();
          ????????????rtn?
          =?true;
          ????????}
          ?catch?(IOException?e)?{
          ????????????e.printStackTrace();
          ????????}


          ????????
          return?rtn;
          ????}


          ????
          /**
          ?????*?修改XML文件
          ?????*?
          @author?cnyqiao@hotmail.com
          ?????*?
          @param?fileName
          ?????*?
          @param?newFileName
          ?????*?@date?Jul?10,?2006?4:03:33?PM
          ?????*?
          @return
          ?????
          */

          ????
          public?boolean?modiXMLFile(String?fileName,?String?newFileName)?{
          ????????
          ????????
          boolean?rtn?=?false;
          ????????
          ????????SAXReader?reader?
          =?new?SAXReader();
          ????????
          try?{
          ????????????Document?document?
          =?reader.read(new?File(fileName));
          ????????????
          //用xpath查找對象
          ????????????List?list?=?document.selectNodes("/catalog/journal/@title");
          ????????????Iterator?itr?
          =?list.iterator();
          ????????????
          while(itr.hasNext())?{
          ????????????????Attribute?attribute?
          =?(Attribute)itr.next();
          ????????????????
          if(attribute.getValue().equals("XML?Zone"))?{
          ????????????????????attribute.setText(
          "Modi?XML");
          ????????????????}

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

          ????????????
          //在journal元素中增加date元素
          ????????????list?=?document.selectNodes("/catalog/journal");
          ????????????itr?
          =?list.iterator();
          ????????????
          if(itr.hasNext())?{
          ????????????????Element?journalElement?
          =?(Element)itr.next();
          ????????????????Element?dateElement?
          =?journalElement.addElement("date");
          ????????????????dateElement.setText(
          "2006-07-10");
          ????????????????dateElement.addAttribute(
          "type",?"Gregorian?calendar");
          ????????????}

          ????????????
          //刪除title接點
          ????????????list?=?document.selectNodes("/catalog/journal/article");
          ????????????itr?
          =?list.iterator();
          ????????????
          while(itr.hasNext())?{
          ????????????????Element?articleElement?
          =?(Element)itr.next();
          ????????????????Iterator?iter?
          =?articleElement.elementIterator("title");
          ????????????????
          while(iter.hasNext())?{
          ????????????????????Element?titleElement?
          =?(Element)iter.next();
          ????????????????????
          if(titleElement.getText().equals("Dom4j?Create?XML?Schema"))?{
          ????????????????????????articleElement.remove(titleElement);
          ????????????????????}

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

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

          ????????????XMLWriter?output;
          ????????????OutputFormat?format?
          =?OutputFormat.createPrettyPrint();
          ????????????
          try?{
          ????????????????output?
          =?new?XMLWriter(new?FileWriter(newFileName),?format);
          ????????????????output.write(document);
          ????????????????output.close();
          ????????????????rtn?
          =?true;
          ????????????}
          ?catch?(IOException?e)?{
          ????????????????e.printStackTrace();
          ????????????}

          ????????????
          ????????????
          ????????}
          ?catch?(DocumentException?e)?{????????????
          ????????????e.printStackTrace();
          ????????}

          ????????
          ????????
          return?rtn;
          ????????
          ????}

          ????
          /**
          ?????*?
          @param?args
          ?????
          */

          ????
          public?static?void?main(String[]?args)?{
          ????????
          ????????ParseXml?parseXml?
          =?new?ParseXml();
          ????????String?fileName?
          =?"d:\\dom4j.xml";
          ????????String?newFileName?
          =?"d:\\modi.xml";
          ????????
          if(parseXml.createXMLFile(fileName))?{
          ????????????System.out.println(
          "Create?XML?File?Success");
          ????????}

          ????????
          ????????
          if(parseXml.modiXMLFile(fileName,?newFileName))?{
          ????????????System.out.println(
          "Modify?XML?File?Success");
          ????????}


          ????}


          }


          在修改XML時,需要用到jaxen-1.1-beta-6.jar,這里http://sourceforge.net/project/showfiles.php?group_id=16035&package_id=14121&release_id=328664下載,記住下那個zip包啊,這個包里的lib下有上面提到的那個文件,放在classpath下即可
          不然會報ClassNotFound異常
          posted on 2006-07-10 17:51 一凡 閱讀(10976) 評論(3)  編輯  收藏 所屬分類: XML

          評論:
          # re: 用DOM4J創建及修改XML文件 2008-11-17 16:26 | yd00
          很好!謝謝,正好用得著,可以轉貼么?  回復  更多評論
            
          # re: 用DOM4J創建及修改XML文件 2008-11-17 16:36 | willpower88
          可以,隨便轉
            回復  更多評論
            
          # re: 用DOM4J創建及修改XML文件 2016-03-17 16:24 |
          打  回復  更多評論
            

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


          網站導航:
           
          <2006年7月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(9)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          學習資源

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 永宁县| 全南县| 英吉沙县| 西乡县| 大名县| 临洮县| 尖扎县| 确山县| 永清县| 宜州市| 石楼县| 马山县| 明星| 平顶山市| 简阳市| 绵竹市| 太保市| 夏邑县| 武胜县| 开封县| 凤山县| 万宁市| 汝南县| 上栗县| 沧源| 宁波市| 石林| 梁山县| 开原市| 行唐县| 平武县| 宁阳县| 班戈县| 贵溪市| 秦安县| 阜宁县| 蓝山县| 防城港市| 内丘县| 西丰县| 新野县|