惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
P
Proofpoint News Feed
H
Hacker News: Front Page
P
Privacy & Cybersecurity Law Blog
月光博客
月光博客
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
Forbes - Security
Forbes - Security
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
博客园 - 叶小钗
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
O
OpenAI News
G
Google Developers Blog
Martin Fowler
Martin Fowler
罗磊的独立博客
S
SegmentFault 最新的问题
T
Tor Project blog
量子位

博客园 - yiling

Eclipse 12大使用技巧 Hibernate 缓存的使用 Quartz应用 Solaris9下安装Oracle10g详细过程(转) Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream - yiling java读取property文件 db2常用命令 ant中利用macrodef来定义可重用的task java查看文件的创建时间 - yiling - 博客园 [转]Oracle exp imp sybase备份服务(backup server)不能启动的处理方法 SQL*PLUS环境输入'&字符'的方法 DB2 Catalog hibernate如何设置一对多cascade Hibernate 一些疑点(转的文章,还不错) Ajax客户端与伺服端之间,使用XML作为数据传送 从找实习的过程中发现了自己的弱点 (转)CAD二次开发简介 WID实施过程中的常见问题
使用JOX实现JavaBean和XML之间的转化
yiling · 2009-07-26 · via 博客园 - yiling
以前不知道有JOX这个包,通过这个包可以很方便的实现XML和JAVABean之间的转化,下面是官网的实例代码,很简单的api一看就会用。
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();
        }
    }
}