luozheng 2005年03月23日
beanreader的用户如何将一个xml内容生成对象
这个xml内容我是根据testbean对象的内容,用beanwriter生成的,但是无法将此内容通过beanreader生成一个含有此内容的testbean对象的实例。请高手指点

xml字符串内容如下:
<?xml version='1.0' ?> <TestBean>
<data>
<TestMember>
<username>user_1</username>
<usersex>female</usersex>
</TestMember>
<TestMember>
<username>user_2</username>
<usersex>female</usersex>
</TestMember>
<TestMember>
<username>user_3</username>
<usersex>male</usersex>
</TestMember>
</data>
<strName>TestBean_strName</strName>
</TestBean>

***************代码如下***********************
 public class TestBean
{
public Vector data=null;
public String strName="TestBean_strName";
public String getStrName()
{
return strName;
}
public Vector getData(){
return data;
};
public void setData(Vector newVal)
{
data=newVal;
}
public void make()
{
TestMember obj1=new TestMember("user_1","female");
TestMember obj2=new TestMember("user_2","female");
TestMember obj3=new TestMember("user_3","male");
data=new Vector();
data.add(obj1);
data.add(obj2);
data.add(obj3);
}
}


public class TestMember
{
private String Username="";
private String Usersex="";

public void setUsername(String newval)
{
Username=newval;
}
public String getUsername(){
return Username;
}

public void setUsersex(String newval){Usersex=newval;}
public String getUsersex()
{
return Usersex;
}

public TestMember(String newval1,String newval2){
Username=newval1;Usersex=newval2;}
}



public class main1 {
public main1() {

}

public void testmethod()
{

TestBean obj=new TestBean();
obj.make();
//TestMember obj=new TestMember("username","sex");
StringWriter outputWriter = new StringWriter();
// System.out.println(obj.getUsername()+";"+obj.getUsersex());
outputWriter.write("<?xml version='1.0' ?>");
try {
BeanWriter beanWriter = new BeanWriter(outputWriter);
// beanWriter.writeXmlDeclaration("<?xml version='1.0' encoding=\"utf-8\" ?>");
// beanWriter.getXMLIntrospector().setAttributesForPrimitives(false);
beanWriter.enablePrettyPrint();
beanWriter.getBindingConfiguration().setMapIDs(false);
beanWriter.write("TestBean", obj);
System.out.print(outputWriter.toString());

//********* beadreader************
// Now convert this to a bean using betwixt
// Create BeanReader
BeanReader beanReader = new BeanReader();

// Configure the reader
// If you're round-tripping, make sure that the configurations are compatible!
beanReader.getXMLIntrospector().setAttributesForPrimitives(false);
beanReader.setMatchIDs(false);
beanReader.registerBeanClass("TestBean", TestBean.class);

// Read Data From person.xml and parse it
InputStream xmlStream = new ByteArrayInputStream(outputWriter.toString().getBytes("utf-8"));
//memberinfo = (MemberInfo) beanReader.parse(xmlStream);
TestBean person = (TestBean)beanReader.parse(xmlStream);


System.out.println(person);


} catch (Exception ex) {
System.out.println(ex.getMessage()+"+++++");
ex.printStackTrace();
}

}




public static void main(String[] args) {
main1 main2 = new main1();

main2.testmethod();



}
}
...全文
164 点赞 收藏 1
写回复
1 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告