xml文件操作的java程序(续)

/**
     * helper方法,查找一个指定的元素
     *
     * @param name 元素名称,格式为 X.Y.Z
     * @return Element 如果找到就返回这个元素,否则返回null
     */
    public Element findOnly(String name)
    {
        //分解元素的名称
        String[] propName = parsePropertyName(name);
        Element element = this.doc.getRootElement();
        //遍历搜索匹配的元素
        for (int i = 0; i < propName.length; i++)
        {
            element = element.getChild(propName[i]);
            if(element == null) return null;
        }
        //找到啦!
        return element;
    }
    /**
     * Saves the properties to disk as an XML document. A temporary file is
     * used during the writing process for maximum safety.
     */
    public synchronized void saveProperties() {
        OutputStream out = null;
        boolean error = false;
        // Write data out to a temporary file first. 数据挖掘实验室
        File tempFile = null;
        try {
            tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
            // Use JDOM"s XMLOutputter to do the writing and formatting. The
            // file should always come out pretty-printed.
            //增加此行使得支持中文    wyl 20021015
            XMLOutputter outputter = new XMLOutputter("", true,"GB2312");
            out = new BufferedOutputStream(new FileOutputStream(tempFile));
            //增加此行,使得xml文件没有空行。 wyl 20021030 数据挖掘交友
            outputter.setTextNormalize(true);
            outputter.output(doc, out);
        }
        catch (Exception e) {
            e.printStackTrace();
            // There were errors so abort replacing the old property file.
            error = true;
        }
        finally {
            try {  out.close();  }
            catch (Exception e) {
                e.printStackTrace();

数据挖掘工具


                error = true;
            }
        }
        // No errors occured, so we should be safe in replacing the old
        if (!error) {
            // Delete the old file so we can replace it.
            if (!file.delete()) {
                System.err.println("Error deleting property file: " +
                        file.getAbsolutePath());
                return; 数据挖掘论坛
            }
            // Rename the temp file. The delete and rename won"t be an
            // automic operation, but we should be pretty safe in general.
            // At the very least, the temp file should remain in some form.
            if (!tempFile.renameTo(file)) {
                System.err.println("Error renaming temp file from " +
                    tempFile.getAbsolutePath() + " to " + file.getAbsolutePath());
            } 数据挖掘交友
        }
    }
   /**
     * Returns an array representation of the given crm property. crm
     * properties are always in the format "prop.name.is.this" which would be
     * represented as an array of four Strings.
     *
     * @param name the name of the crm property.
     * @return an array representation of the given crm property.
     */
    public String[] parsePropertyName(String name) {
        // Figure out the number of parts of the name (this becomes the size
        // of the resulting array).
        int size = 1;
        for (int i=0; i<name.length(); i++) { 数据挖掘工具
            if (name.charAt(i) == ".") {
                size++;
            }
        }
        String[] propName = new String[size];
        // Use a StringTokenizer to tokenize the property name.
        StringTokenizer tokenizer = new StringTokenizer(name, ".");
        int i = 0;
        while (tokenizer.hasMoreTokens()) {
            propName[i] = tokenizer.nextToken();
            i++;
        } 数据挖掘研究院
        return propName;
    }
    private String[] parseAttributeName(String name)
    {
        // Figure out the number of parts of the name (this becomes the size
        // of the resulting array).
        if(name==null) return null;
        
        int size = 1;
        for (int i=0; i<name.length(); i++)
        {
            if (name.charAt(i) == "=")
            {
                size++;

数据挖掘研究院


            }
        }
        if(size!=2) return null;
        String[] attrName = new String[size];
        // Use a StringTokenizer to tokenize the property name.
        StringTokenizer tokenizer = new StringTokenizer(name, "=");
        for(int i=0;tokenizer.hasMoreTokens();i++)
            attrName[i] = tokenizer.nextToken();
        return attrName;
   }
    /**
    *    @param element element==null start from root
    *    @param attName attribute name

数据挖掘交友


    *    @return attribute value of this element
    *    @author qiuss
    *    @date 2001.11.08
    */        
            
    public String getAttribute(Element element,String attName)
    {
        if(attName==null) return null;
        // Search for this property by traversing down the XML heirarchy.
        if(element==null) element = doc.getRootElement();
        Attribute att;
        String value;
        if((att=element.getAttribute(attName))!=null)

数据挖掘工具


            value = att.getValue();
        else
            return null;
        if ("".equals(value))
        {
            return null;
        }
        else
        {
            return value.trim();
        }
    }     
    // qiuss 2001.11.08

    public String getAttribute(String eleName,String attName)
    {

数据挖掘论坛


        String[] propName = parsePropertyName(eleName);
        // Search for this property by traversing down the XML heirarchy.
        Element element = doc.getRootElement();
        for (int i = 0; i < propName.length; i++)
        {
            element = element.getChild(propName[i]);
            if (element == null)
            {
                    // This node doesn"t match this part of the property name which
                    // indicates this property doesn"t exist so return null. 数据挖掘研究院
                    return null;
            }
        }
        // At this point, we found a matching property, so return its value.
        // Empty strings are returned as null.
        String value ="";
        try{
            value= element.getAttribute(attName).getValue();
        }
        catch(Exception e)
        {
            value ="";
        }
        if ("".equals(value))
        {
            return "";
        }
        else
        {
            return value.trim();
        }
    }
    public static void main(String[] args)
    {
        XMLProperty xmltree = new XMLProperty("D:/project/bicp-ivr/crm_config.xml");
        //存在则更改元素的属性值
        xmltree.setProperty("system.connection.username2","type","中文测试");
        //不存在则新建一个元素
        xmltree.setProperty("system.connection.用户名称","ecom4");
        xmltree.setProperty("system.connection.用户名称","type","中文测试2222");
        //删除一个xml
//        xmltree.deleteProperty("system.connection.username2");
        String db_url= xmltree.getProperty("system.connection.db_url");
        String db_url_id="";
        db_url_id=xmltree.getAttribute("system.information.db_url","remark");
        Debug.println("db_url="+db_url);
        Debug.println("db_url_id="+db_url_id);
//        String names[] = xmltree.parsePropertyName("style.body");
//        for(int i=0;i<names.length;i++)
//        {
//            Debug.println("i="+i+" "+names[i]);
//        }
        String child[] = xmltree.getChildrenProperties("style.body");
        for(int j=0;j<child.length;j++)
        {
            Debug.println("j="+j+" "+child[j]);
            String child2[]= xmltree.getChildrenProperties("system.tracelog."+child[j]);
            for(int k=0;k<child2.length;k++)
            { 数据挖掘研究院
                Debug.println("k="+k+" "+child2[k]);
            }
        }

        //System.out.println("Hello World!");

    }
}
[数据挖掘专家] [数据挖掘研究院] [数据挖掘论坛] [数据挖掘实验室]
上一篇:没有了
下一篇:XML实现异构数据库间转换的实现与分析(转)
最新评论共有 0 位网友发表了评论 , 查看所有评论
发表评论( 不能超过250字,需审核,请自觉遵守互联网相关政策法规。 )
匿名?
数据挖掘网站导航 数据挖掘论坛导航
  • 数据挖掘工具
  • 数据挖掘论坛
  • DataCruncher - Cognos
  • MineSet - MathSoft
  • Intelligent Miner - GainSmarts
  • Sqlserver - SAS - Clementine
  • CART - Weka - WizSoft
  • NeuroShell - ModelQuest
  • data mining tools - Darwin
  • 数据挖掘交友
  • 数据挖掘博客
  • 数据挖掘工具
  • 数据挖掘资源
  • 数据挖掘技术算法
  • 数据挖掘相关期刊、会议
  • 研究院联盟合作专区
  • 数据挖掘基础与相关技术
  • 数据挖掘厂商与就业
  • 数据挖掘研究者乐园
  • 知名厂商数据挖掘工具资料
  • 国内数据挖掘实验室
  • Foreign Data Mining Lab
  • 热点关注
  • Java学生成绩管理系统源代码
  • Servlet动态产生JPEG图像的例子
  • JSP数据导出到EXCEL简便方法
  • JSP像乌云般挡住了JSF的光芒
  • Java EE5.0时代来临,金蝶Apusic抢先撞线!
  • JSP不是简化的Java
  • 在JSP中如何实现MD5加密
  • JSP/Servlet/JSF:标签库的深入研究
  • jsp计数器制作手册
  • jsp页面显示数据导出到excel表中
  • 论坛最新话题
  • Foundations of Statistical Natural Langu
  • Game Theory meet Data Mining: A Recent P
  • System Building: How does it help or hin
  • 数据挖掘与Clementine培训
  • 新手报到
  • 求 SASEM 客户流失预测分析
  • 数据挖掘工程师/搜索研究院—北京——无线
  • 数据挖掘入门介绍(如何着手数据挖掘)
  • Information Overload Survey Results
  • The INEX 2005 Workshop on Element Retrie
  • 相关资讯
  • Java EE5.0时代来临,金蝶Apusic抢先撞线!
  • JSP像乌云般挡住了JSF的光芒
  • jsp计数器制作手册
  • 面向对象编程,我的思想(5)
  • jsp读取大对象CLOB并生成xml文件示例
  • JSP开发前菜鸟设置篇
  • JRun3.0配合IIS的安装全过程
  • win2000下jsp平台搭建的简单过程
  • IIS6和Tomcat5的整合
  • Windows下JSP开发环境的配置
  • 数据挖掘实验室资料
  • 数据挖掘博客地址
  • 数据挖掘实验室网站地址
  • Prepare for Medicare audits by using dat
  • 注册成为SAS用户与爱好者俱乐部会员
  • 水南梅
  • 明日烟
  • 新人报道
  • 下载
  • 厦门服务器托管,450元/月—0592-5177319 高
  • 买空间送域名--0592-5177319 高静