把xml转为成javaBean javaBean转为成xml
来源:互联网 发布:平面设计好还是java好 编辑:程序博客网 时间:2024/06/07 18:55
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();
}
}
}
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();
}
}
}
- 把xml转为成javaBean javaBean转为成xml
- xml转为javaBean
- xStream 将xml转为javaBean
- HttpEntity转为List<JavaBean>
- javaBean转为json
- 使用dom4j把XML文件解析成JavaBean
- 将javaBean转换成XML
- 将javaBean转换成XML
- 怎么把xml文件转为java文件
- 通过JAXB把XML文件转成JavaBean
- 用XStream读取XML解析成javabean
- Xstream 来实习JavaBean转换成XML
- xStream(javabean转换成xml)
- ASP.NET中把xml转为dataset与xml字符串转为dataset及dataset转为xml的代码
- SAX 解析XML 将xml转换成javaBean
- JavaBean转Xml与Xml转JavaBean
- Gson将负载json字符串转为Javabean
- 利用ObjectMapper将javabean转为json
- 实现像sum对整数一样,字符串按某列group by来组合字符串列
- 从myspace数据库看分布式系统数据结构变迁
- 开放平台:StackMob for iOS
- linu型 内核中arm中断流程详细分析
- 抛弃中断bottom half,使用中断线程吧
- 把xml转为成javaBean javaBean转为成xml
- Oracle Nologging And Append
- Hadoop集群搭建之SSH无密码登录配置错误解析
- Git 总结
- Nginx下防御HTTP GET FLOOD(CC)攻击
- linux线程相关函数
- 回调函数的理解
- 万物简史
- hdu Max Sum Plus Plus(最大m段子段和)