欢迎光临
我们一直在努力

xml文件操作的java程序(续)-JSP教程,Java与XML

建站超值云服务器,限时71元/月

/**
     * 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 jdoms 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 wont 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 doesnt match this part of the property name which
                    // indicates this property doesnt 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!");

    }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » xml文件操作的java程序(续)-JSP教程,Java与XML
分享到: 更多 (0)

相关推荐

  • 暂无文章