免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1662 | 回复: 2
打印 上一主题 下一主题

jdom的使用经验 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-03 10:11 |只看该作者 |倒序浏览
使用jdom做了一个项目,自己使用了一些核心类。为了方便大家也为了自己以后不要忘记,发表在这里。希望大家指点:
我的核心代码
package http.com.util.xml;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;


/**
* @author liqinglin
*        xml解析读类
*        任务1:读取xml配置文件格式化成(标准的xml对象)
*        任务2:读取标准的jdom对象转化为字符串。
*        任务3:读取vo数据格式化成(标准的xml对象)、字符串。
*        任务6:读取容器中的数据对象转化为(标准的xml对象)
*/
public class XmlRead
{               
                /**
                 * 读取xml配置文件格式化成标准的jdom对象
                 * @param: fileName:文件的路径和名字
                 * @return:标准的jdom对象
                 */
                public Document returnDom_dom(String fileName)
                {
                        SAXBuilder sb = new SAXBuilder(false);
                        Document doc=null;               
                        try
                        {
                                doc = sb.build(fileName);
                        } catch (JDOMException e1)
                        {
                                e1.printStackTrace();
                        } catch (IOException e2)
                        {
                                e2.printStackTrace();
                        }                                               
                        return doc;
                }
                /**
                 * 输出设计
                 * 将jdom对象转化为String字符串
                 * @param domocument对象
                 * @return
                 */
                public  String returnString_dom(Document dom)
                {
                        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
                        Format format = outputter.getFormat();
                        //format.setEncoding("GB2312";//默认UTF-8
                        format.setExpandEmptyElements(true);
                        outputter.setFormat(format);
                        //以文件的形式输出在src目录下
//                        try
//                        {
//                                outputter.output(dom, new FileOutputStream("jdomcompany.xml");
//                        } catch (FileNotFoundException e)
//                        {
//                               
//                                e.printStackTrace();
//                        } catch (IOException e) {
//                               
//                                e.printStackTrace();
//                        }
                        //返回字符串操作               
                        return outputter.outputString(dom);
                }
                /**
                 * 输入数据对象返回Document对象一部分Element对象
                 * @param root:根节点
                 * @param classPath:包路径和类名称
                 * @return
                 */
                private Element returnEl_vo(String root,Object o)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                {
                        Class voclass=null;;
                        try
                        {
                                voclass = Class.forName(o.getClass().getName());
                        } catch (ClassNotFoundException e)
                        {
                                e.printStackTrace();
                        }
                        //类属性字符串数组
                        Field[] f=voclass.getDeclaredFields();
                        Element father = new Element(root);
                        //遍历属性
                        for(int i=0;i!=f.length;i++)
                        {                               
                               
                                String key=f[i].getName();
                                String value="";
                                Element child = new Element(key);
                                //调用get方法
                                String prop = Character.toUpperCase(key.charAt(0)) + key.substring(1);
                                String getM="get" + prop;
                                Class[] types = new Class[] {};
                                Method method=null;
                                try
                                {
                                        method = o.getClass().getMethod(getM, types);
                                } catch (SecurityException securityException)
                                {                               
                                        securityException.printStackTrace();
                                } catch (NoSuchMethodException noSuchMethodException)
                                {                               
                                        noSuchMethodException.printStackTrace();
                                }
                                Object result=null;
                                try
                                {
                                        result = method.invoke(o, new Object[0]);
                                } catch (IllegalArgumentException illegalArgumentException)
                                {                       
                                        illegalArgumentException.printStackTrace();
                                } catch (IllegalAccessException illegalAccessException)
                                {                       
                                        illegalAccessException.printStackTrace();
                                } catch (InvocationTargetException invocationTargetException)
                                {                                 
                                        invocationTargetException.printStackTrace();
                                }
                                value=(String)result.toString();
                                child.addContent(value);
                                father.addContent(child);                               
                        }                       
                        return father;
                }
                /**
                 * 输入数据对象容器,返回Document对象
                 * @param listRoot:总根节点
                 * @param obRoot:每一数据对象节点
                 * @param list:封状数据对象的容器
                 * @return:Document对象
                 */
                public Document returnDom_list(String listRoot,String obRoot,ArrayList list)
                {
                        Element father = new Element(listRoot);
                        for(int i=0;i!=list.size();i++)
                        {
                                Object o=(Object)list.get(i);
                                father.addContent(returnEl_vo(obRoot,o));
                        }                       
                        return new Document(father);
                }
                /**
                 * 输入数据对象vo
                 * @param root:根节点
                 * @param classPath:包路径和类名称
                 * @return 返回Document对象
                 */
                public Document returnDom_vo(String root,Object o)
                {
                        return new Document(returnEl_vo(root,o));
                }
}
解释待续。

论坛徽章:
0
2 [报告]
发表于 2005-12-03 10:31 |只看该作者

测试类

package test;
import java.io.IOException;
import java.util.ArrayList;

import org.jdom.JDOMException;

import http.com.util.xml.XmlRead;
/**
* @author liqinglin
*        测试main函数
*/
public class Test
{

        public static void main(String[] args) throws JDOMException, IOException
        {
                ArrayList list =new ArrayList();
                for(int i=0;i!=5;i++)
                {
                        TestVo vo=new TestVo();
                        vo.setD1(i);
                        vo.setF1(i);
                        vo.setI1(i);
                        vo.setS1("知音少,琴断有谁听!"+i);
                        list.add(vo);
                }
                XmlRead r=new XmlRead();               
                System.out.println(r.returnString_dom(r.returnDom_list("root","data",list)));
        }
//        public static void main(String[]args) throws JDOMException, IOException
//        {
//                XmlRead xd=new XmlRead();
//                System.out.println(xd.returnString_dom(xd.returnDom_dom("src/xml/bus.xml")));
//        }       
       
}



vo
package test;

/**
* @author liqinglin
*        测试vo
*/
public class TestVo
{
        private String s1;
        private int i1;
        private double d1;
        private float f1;
       
        /**
         * @return
         */
        public double getD1() {
                return d1;
        }

        /**
         * @return
         */
        public float getF1() {
                return f1;
        }

        /**
         * @return
         */
        public int getI1() {
                return i1;
        }

        /**
         * @return
         */
        public String getS1() {
                return s1;
        }

        /**
         * @param d
         */
        public void setD1(double d) {
                d1 = d;
        }

        /**
         * @param f
         */
        public void setF1(float f) {
                f1 = f;
        }

        /**
         * @param i
         */
        public void setI1(int i) {
                i1 = i;
        }

        /**
         * @param string
         */
        public void setS1(String string) {
                s1 = string;
        }

}

论坛徽章:
0
3 [报告]
发表于 2005-12-03 10:46 |只看该作者
lz 使用 [code] 把代码给引用起来比较容易看..
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP