Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > use JOX framework let javabean to xml
【标  题】:use JOX framework let javabean to xml
【关键字】:use,JOX,framework,let,javabean,to,xml
【来  源】:http://blog.csdn.net/moto118118/archive/2007/04/18/1569502.aspx

use JOX framework let javabean to xml

Your Ad Here

JOX Documentation

The Javadoc API documentation is available in the JOX distribution, or you can view it online here.

The Quick & Dirty JOX Tutorial

JOX is extremely easy to use. Assume you have the following Java Bean with several properties including an indexed property and a property whose value is another object.

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();    }}

and you have the following XML file:

<?xml version="1.0"?><MarkTest><thingies>Moe</thingies><thingies>Larry</thingies><thingies>Curly</thingies><thingies>Shemp</thingies><thingies>Curly Joe</thingies><foo>5</foo><baz>6/25/00 12:46 AM</baz><bar>This is the bar value</bar><sub><age>35</age><name>Mark</name></sub></MarkTest>

The following program reads in the XML file and stores its values into TestBean:

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();        }    }}

All you do is create a FileInputStream or FileReader to read the XML file and wrap a JOXBeanInputStream or JOXBeanReader around the file stream. Then you tell JOX to read an object and tell it the class of the object.

Writing a bean out to XML is just as easy:

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[] {                "Moe", "Larry", "Curly", "Shemp", "Curly Joe" });            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();        }    }}

You just put some values in the bean, create an output stream for writing the XML, slap a JOXBeanOutputStream or JOXBeanWriter around the output stream and write the object. Notice that you need to put the root tag name for the XML file when you write out the XML. In the future, you won't have to do this if you have a DTD and JOX can figure out the root tag from the DTD.

For a final example, assume you have the following DTD:

<?xml version="1.0" encoding="ISO-8859-1"?><!ELEMENT MarkTest (Thingies*, foo?, BAR?, baz? S-U-B?)><!ELEMENT Thingies #PCDATA><!ELEMENT foo #PCDATA><!ELEMENT BAR #PCDATA><!ELEMENT baz #PCDATA><!ELEMENT S-U-B (age)><!ELEMENT age #PCDATA><!ATTLIST S-U-B    name CDATA "">

The following program reads in the DTD and passes it to JOX to help JOX format the output:

package com.wutka.jox.test;import com.wutka.jox.*;import com.wutka.jox.dtd.*;import java.io.*;public class TestSerDTD{    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[] {                "Moe", "Larry", "Curly", "Shemp", "Curly Joe" });            TestSubbean sub = new TestSubbean();            sub.setName("Mark");            sub.setAge(35);            b.setSub(sub);            FileOutputStream fileOut = new FileOutputStream("bean.xml");            FileReader reader = new FileReader("testbean.dtd");            Parser dtdParser = new Parser();            DTD dtd = dtdParser.parse(reader);            reader.close();            JOXBeanOutputStream joxOut = new JOXBeanOutputStream(dtd, fileOut);            joxOut.writeObject("MarkTest", b);            joxOut.close();        }        catch (Exception exc)        {            exc.printStackTrace();        }    }}
 
java静态构造器:【上一篇】
Eclipse下myeclipse和lomboz插件的应用:【下一篇】
【相关文章】
  • 用JSP调用以Web应用形式部署在Tomcat 5.5中的SCA服务组件的例子
  • Development tools for C++ on Linux development environment
  • 安装appfuse
  • hibernate 配置文件之hibernate.cfg.xml说明
  • hibernate的表结构以及表之间的关系的 *.hbm.xml
  • Net Framework 2.0 事务处理
  • 用DropDownList控件绑定XML数据实现省市区三级联动
  • 利用复合的javabean构造基于jasperreports的子报表
  • 一种Map与JavaBean可配置转换的实现
  • Java开源旅程之二 MyEclipse上配置服务器(Tomcat为例)
  • 【随机文章】
  • 对不住观众
  • 队列操作
  • 妙用磁盘配额 让黑客无从下手
  • FreeBSD/i386 6.0-RELEASE Release Notes
  • 如何获取多核、多cpu系统中指定cpu的序列号
  • 利用 gettext 来实现 PHP 的国际化编程
  • 看图详解 Ado Access 联结字符串。
  • 好用的Linux小工具:Autofs
  • 微软挖角IBM ,怎么看?
  • 实现Windows, Unix, Linux多系统并存(2)
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.