DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 通過com.wutka.jox輕松實現JavaBeans與XML的相互轉換
通過com.wutka.jox輕松實現JavaBeans與XML的相互轉換
編輯:XML詳解     

不用復雜的代碼,就可以讓您的JavaBeans自己控制到XML文件的相互轉化。本文展示了怎樣通過JOX來實現從JavaBeans到XML文件的相互轉換。

   為了靈活的滿足Web應用和Web services需求的變化,Java和XML的輕便性和可擴展性使它們成為解決這一問題的理想選擇。SAX (Simple API for XML), DOM (document.nbspObject Model), XSL (Extensible Stylesheet Language), XSLT (XSL Transformations), SOAP (Simple Object Access Protocol), and BML (Bean Markup Language)是XML領域內的相關技術。本文集合了Java和XML輕便和可擴展的優點,但又不需要開發人員了解上述的相關技術。

   在Java分布式應用中使用遠程方法調用(RMI),而不是直接用底層的socket或其它網絡鏈接代碼。EJB技術也使開發人員從transaction,recovery, activation等底層機制中解放出來。同樣,使用本文的JavaBean-XML映射組件,開發人員也不用直接處理與XML有關的APIs。

  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>http://www.5a520.cn 小說520網</thingIEs>
<thingies>http://www.bt285.cn BT下載</thingIEs>
<thingies>http://www.bt285.cn/caj/ caj下載</thingIEs>
<thingies>http://www.bt285.cn/baolimotuo/ 暴力摩托</thingIEs>
<thingies>http://www.bt285.cn/btjinglin bt精靈</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[] {
                "http://www.5a520.cn 小說520網", "http://www.bt285.cn BT下載", "http://www.bt285.cn/caj/ caj下載", "http://www.bt285.cn/baolimotuo/ 暴力摩托", "http://www.bt285.cn/btjinglin bt精靈" });
            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();
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
}



XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved