把xml转为成javaBean javaBean转为成xml

    本文介绍了一个具体的例子,展示了如何通过JavaBean实现XML文件的读取和写入。首先定义了一个JavaBean类,并在XML文件中填充了相应的属性值。接着,通过Java程序实现了从XML到JavaBean的转换及从JavaBean到XML的转换。

    摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    1.先定义javaBean
    package com.wutka.jox.test;

    import com.wutka.jox. * ;
    import java.util. * ;

    public class TestBean implements java.io.Serializable
    {
    protected int foo;
    protected String bar;
    protected java.util.Date baz;
    protected Vector thingies;
    protected TestSubbean subbean;

    public TestBean()
    {
    bar = "" ;
    baz = new Date();
    thingies = new Vector();
    }

    public int getFoo() { return foo; }
    public void setFoo( int aFoo) { foo = aFoo; }

    public String getBar() { return bar; }
    public void setBar(String aBar) { bar = aBar; }

    public java.util.Date getBaz() { return baz; }
    public void setBaz(java.util.Date aBaz) { baz = aBaz; }

    public TestSubbean getSub() { return subbean; }
    public void setSub(TestSubbean aSub) { subbean = aSub; }

    public String[] getThingies()
    {
    String[] retThingies = new String[thingies.size()];
    if (thingies.size() > 0 ) thingies.copyInto(retThingies);

    return retThingies;
    }

    public void setThingies(String[] newThingies)
    {
    thingies = new Vector(newThingies.length);
    for ( int i = 0 ; i < newThingies.length; i ++ )
    {
    thingies.addElement(newThingies[i]);
    }
    }

    public String getThingies( int i)
    {
    return (String) thingies.elementAt(i);
    }

    public void setThingies( int i, String thingy)
    {
    thingies.setElementAt(thingy, i);
    }

    public String toString()
    {
    StringBuffer ret = new StringBuffer(
    " foo= " + foo + " ;bar= " + bar + " ;baz= " + baz.toString() +
    " ;thingies= " );
    for ( int i = 0 ; i < thingies.size(); i ++ )
    {
    if (i > 0 ) ret.append( " , " );
    ret.append((String) thingies.elementAt(i));
    }

    ret.append( " ;sub= " );
    ret.append(subbean.toString());

    return ret.toString();
    }
    }

    2.xml文件


    <? xml version="1.0" ?>
    < MarkTest >
    < thingies >网 </ thingies >
    < thingies > 载 </ thingies >
    < thingies > 下 </ thingies >
    < thingies > 托 </ thingies >
    < thingies > 灵 </ thingies >
    < foo > 5 </ foo >
    < baz > 6/25/00 12:46 AM </ baz >
    < bar > This is the website value </ bar >
    < sub >
    < age > 1 </ age >
    < name > wangdei </ name >
    </ sub >
    </ MarkTest >


    下面的程序是把xml转为成javaBean
    package com.wutka.jox.test;

    import com.wutka.jox. * ;
    import java.io. * ;

    public class TestDeser
    {
    public static void main(String[] args)
    {
    try
    {
    FileInputStream in = new FileInputStream( " bean.xml " );

    JOXBeanInputStream joxIn = new JOXBeanInputStream(in);

    TestBean testBean = (TestBean) joxIn.readObject(
    TestBean. class );

    System.out.println(testBean);
    }
    catch (Exception exc)
    {
    exc.printStackTrace();
    }
    }
    }



    下面的程序是把javaBean转为成xml
    package com.wutka.jox.test;

    import com.wutka.jox. * ;
    import java.io. * ;

    public class TestSer
    {
    public static void main(String[] args)
    {
    try
    {
    TestBean b = new TestBean();
    b.setFoo( 5 );
    b.setBar( " This is the bar value " );
    b.setThingies( new String[] {
    " 网 " ,
    " 载 " ,
    " 下 " ,
    " 托 " ,
    " 灵 "
    } );
    TestSubbean sub = new TestSubbean();
    sub.setName( " Mark " );
    sub.setAge( 35 );
    b.setSub(sub);

    FileOutputStream fileOut = new FileOutputStream( " bean.xml " );
    JOXBeanOutputStream joxOut = new JOXBeanOutputStream(fileOut);

    joxOut.writeObject( " MarkTest " , b);

    joxOut.close();


    }
    }
    }

    登录后您可以享受以下权益:

    ×
    评论 1
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回顶部