灵魂-放水

为学日益,为道日损。

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 放水老倌 阅读(394) 评论(0)  编辑  收藏 所属分类: XML

只有注册用户登录后才能发表评论。


网站导航: