靈魂-放水

          為學日益,為道日損。

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            296 Posts :: 10 Stories :: 274 Comments :: 0 Trackbacks

          ?程序代碼片-類方法:用到jdom包
          public void removeElement(String path,String catalog){
          ???????SAXBuilder sb = new SAXBuilder();
          ?? try
          ????? {???????
          ?????? Document doc = sb.build(path);
          ?????? Element root = doc.getRootElement();
          ?????? Element subroot=root.getChild("entity");
          ??????
          ?????? Element content=subroot.getChild("contents");
          ??? ?
          ??? ? List entities=content.getChildren("entity");
          ??? ? for(int j=0;j<entities.size();j++)
          ??? ? {
          ??? ?? Element _element=(Element)entities.get(j);
          ??? ?? if(_element.getChild("nick").getText().equals(catalog))
          ??? ?? {
          ??? ??? System.out.println(_element.getChild("nick").getText());
          ??? ??? System.out.println(_element.getChild("description").getText());
          ?????? ??? content.removeContent(_element);
          ?????????? //_element.removeContent(_element);
          ?????????? }
          ??? ? }
          ??????
          ???????? Format format = Format.getCompactFormat();

          ???????? format.setEncoding("UTF-8"); //設置xml文件的字符為UTF-8
          ???????? format.setIndent("??? "); //設置xml文件的縮進為4個空格

          ???????? XMLOutputter xmlOutput = new XMLOutputter(format);
          ???????? xmlOutput.output(root, new FileOutputStream(path));
          ????? }
          ??? catch(Exception e)
          ??? {
          ????????? e.printStackTrace();
          ??? }
          ???? }
          XML文件片:
          ?<entity>
          ??????? <description>文檔目錄管理</description>
          ??????? <imageBase>images/globe.gif</imageBase>
          ??????? <imageOpen>images/globe_selected.gif</imageOpen>
          ??????? <contents>
          ??????????? <entity>
          ??????????????? <nick>1</nick>
          ??????????????? <description>檔案管理程序</description>
          ??????????????? <imageBase>images/book.gif</imageBase>
          ??????????????? <imageOpen>images/bookOpen.gif</imageOpen>
          ??????????????? <contents>
          ??????????????????? <entity>
          ??????????????????????? <nick>1</nick>
          ??????????????????????? <description>國家Linux技術培訓與推廣中心檔案查詢登記表</description>
          ??????????????????????? <imageBase>images/paper.gif</imageBase>
          ??????????????????????? <imageOpen>images/paper.gif</imageOpen>
          ??????????????????????? <title>國家Linux技術培訓與推廣中心檔案查詢登記表</title>
          ??????????????????????? <dir>doc/1/1.doc</dir>
          ??????????????????? </entity>
          ??????????????????? <entity>
          ??????????????????????? <nick>2</nick>
          ??????????????????????? <description>國家Linux技術培訓與推廣中心檔案管理程序</description>
          ??????????????????????? <imageBase>images/paper.gif</imageBase>
          ??????????????????????? <imageOpen>images/paper.gif</imageOpen>
          ??????????????????????? <title>國家Linux技術培訓與推廣中心檔案管理程序</title>
          ??????????????????????? <dir>doc/1/2.doc</dir>
          ??????????????????? </entity>
          ??????????????????? <entity>
          ??????????????????????? <nick>3</nick>
          ??????????????????????? <description>lnic人員管理辦法</description>
          ??????????????????????? <imageBase>images/paper.gif</imageBase>
          ??????????????????????? <imageOpen>images/paper.gif</imageOpen>
          ??????????????????????? <title>lnic人員管理辦法</title>
          ??????????????????????? <dir>doc/1/3.txt</dir>
          ??????????????????? </entity>
          ??????????????? </contents>
          ??????????? </entity>
          ??????????? <entity>
          ??????????????? <nick>2</nick>
          ??????????????? <description>對外業務</description>
          ??????????????? <imageBase>images/book.gif</imageBase>
          ??????????????? <imageOpen>images/bookOpen.gif</imageOpen>
          ??????????????? <contents>
          ??????????????????? <entity>
          ??????????????????????? <nick>1</nick>
          ??????????????????????? <description>國家Linux技術培訓與推廣中心供應商調查報告</description>
          ??????????????????????? <imageBase>images/paper.gif</imageBase>
          ??????????????????????? <imageOpen>images/paper.gif</imageOpen>
          ??????????????????????? <title>國家Linux技術培訓與推廣中心供應商調查報告</title>
          ??????????????????????? <dir>doc/2/1.doc</dir>
          ??????????????????? </entity>
          ??????????????????? <entity>
          ??????????????????????? <nick>2</nick>
          ??????????????????????? <description>國家Linux技術培訓與推廣中心國有資產申購單</description>
          ??????????????????????? <imageBase>images/paper.gif</imageBase>
          ??????????????????????? <imageOpen>images/paper.gif</imageOpen>
          ??????????????????????? <title>國家Linux技術培訓與推廣中心國有資產申購單</title>
          ??????????????????????? <dir>doc/2/2.doc</dir>
          ??????????????????? </entity>
          ??????????????????? <entity>
          ??????????????????????? <nick>3</nick>
          ??????????????????????? <description>國家Linux技術培訓與推廣中心合格供應商名錄</description>
          ??????????????????????? <imageBase>images/paper.gif</imageBase>
          ??????????????????????? <imageOpen>images/paper.gif</imageOpen>
          ??????????????????????? <title>國家Linux技術培訓與推廣中心合格供應商名錄</title>
          ??????????????????????? <dir>doc/2/3.doc</dir>
          ??????????????????? </entity>
          ??????????????????? <entity>
          ??????????????????????? <nick>4</nick>
          ??????????????????????? <description>國家Linux技術培訓與推廣中心進貨質量驗收計劃</description>
          ??????????????????????? <imageBase>images/paper.gif</imageBase>
          ??????????????????????? <imageOpen>images/paper.gif</imageOpen>
          ??????????????????????? <title>國家Linux技術培訓與推廣中心進貨質量驗收計劃</title>
          ??????????????????????? <dir>doc/2/4.doc</dir>
          ??????????????????? </entity>
          ??????????????????? <entity>
          ??????????????????????? <nick>5</nick>
          ??????????????????????? <description>國家Linux技術培訓與推廣中心培訓協議書</description>
          ??????????????????????? <imageBase>images/paper.gif</imageBase>
          ??????????????????????? <imageOpen>images/paper.gif</imageOpen>
          ??????????????????????? <title>國家Linux技術培訓與推廣中心培訓協議書</title>
          ??????????????????????? <dir>doc/2/5.doc</dir>
          ??????????????????? </entity>
          ??????????????????? <entity>
          ??????????????????????? <nick>6</nick>
          ??????????????????????? <description>國家Linux技術培訓與推廣中心外購物品驗收報告</description>
          ??????????????????????? <imageBase>images/paper.gif</imageBase>
          ??????????????????????? <imageOpen>images/paper.gif</imageOpen>
          ??????????????????????? <title>國家Linux技術培訓與推廣中心外購物品驗收報告</title>
          ??????????????????????? <dir>doc/2/6.doc</dir>
          ??????????????????? </entity>
          ??????????????????? <entity>
          ??????????????????????? <nick>7</nick>
          ??????????????????????? <description>lnic人員器材管理辦法</description>
          ??????????????????????? <imageBase>images/paper.gif</imageBase>
          ??????????????????????? <imageOpen>images/paper.gif</imageOpen>
          ??????????????????????? <title>lnic人員器材管理辦法</title>
          ??????????????????????? <dir>doc/2/7.doc</dir>
          ??????????????????? </entity>
          ??????????????? </contents>
          ??????????? </entity>
          ????????錯誤出現在: 1、content.removeContent(_element);
          ??????????2、 //_element.removeContent(_element);

          用第2種方法總達不到效果,搞了半天才更正了錯誤。

          posted on 2006-05-25 18:22 放水老倌 閱讀(405) 評論(0)  編輯  收藏 所屬分類: XML
          主站蜘蛛池模板: 准格尔旗| 南昌市| 台东市| 邹城市| 高雄县| 临安市| 赞皇县| 芦溪县| 蕲春县| 上蔡县| 乐至县| 渝中区| 阿拉善右旗| 博客| 游戏| 宜宾市| 岑巩县| 荆门市| 三明市| 衡山县| 精河县| 周口市| 阜南县| 土默特右旗| 多伦县| 金川县| 徐州市| 得荣县| 海兴县| 龙山县| 阳曲县| 涪陵区| 沙湾县| 慈溪市| 驻马店市| 怀集县| 沾益县| 佛坪县| 仙居县| 子洲县| 白沙|