前一篇见:
使用Betwixt将javaBean生成xml
当有一对多的关系时,生成
xml
:
package
betwixt;
import
java.util.ArrayList;
import
java.util.List;
public
class
NoteBean {
private
String
toWho
;
private
String
fromWho
;
private
String
title
;
private
String
note
;
private
List<User>
details
=
new
ArrayList <User>();
…….
public
class
User {
private
String
name
;
private
String
password
;
/**
*
@return
the
name
*/
public
String getName() {
return
name
;
}
/**
*
@param
name
the
name
to
set
*/
public
void
setName(String name) {
this
.
name
= name;
}
public
User(String name) {
this
.
name
= name;
}
/**
*
@return
the
password
*/
public
String getPassword() {
return
password
;
}
/**
*
@param
password
the
password
to
set
*/
public
void
setPassword(String password) {
this
.
password
= password;
}
}
UserBean.betwixt
<info primitiveTypes="element">
<element name="note">
<element name="detailsaaa" > // name
表示生成多个元素时父结点名
<element name="useraa" property="details"/> //name
子结点名,
property
为
UserBean
中的属性
</element>
<addDefaults />
</element>
</info>
User.betwixt
<info primitiveTypes="element">
<element name="note">
<element name="subname" property="name" />
<element name="password" property="password" />
<addDefaults />
</element>
</info>
测试:
public
class
WriteExampleApp {
/**
*
Create
an
example
bean
and
then
convert
it
to
xml.
*/
public
static
final
void
main(String [] args)
throws
Exception {
// Start by preparing the writer
// We'll write to a string
StringWriter outputWriter =
new
StringWriter();
// Betwixt just writes out the bean as a fragment
// So if we want well-formed xml, we need to add the prolog
outputWriter.write(
"<?xml version='1.0' ?>"
);
// Create a BeanWriter which writes to our prepared stream
BeanWriter beanWriter =
new
BeanWriter(outputWriter);
// Configure betwixt
// For more details see java docs or later in the main documentation
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(
false
);
// beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new org.apache.commons.betwixt.strategy.HyphenatedNameMapper());
beanWriter.getBindingConfiguration().setMapIDs(
false
);
beanWriter.enablePrettyPrint();
//
启用缩进格式
.
beanWriter.setEndTagForEmptyElement(
true
);
//true ,
当为元素时
,<aa></aa>,
不是
<aa/>
beanWriter.setWriteEmptyElements(
false
);
// false ,
表示如果是空元素
,
就不显示
.
比如如果
user
中的
password
为空,就不显示
// beanWriter.setIndent("/t");
beanWriter.writeXmlDeclaration(
""
);
NoteBean test=
new
NoteBean();
test.setToWho(
"fdfdf"
);
test.setFromWho(
"frof"
);
test.setTitle(
"tesdd"
);
test.setNote(
"fdfddf"
);
List<User> list=
new
ArrayList <User>();
list.add(
new
User(
"aaa"
));
list.add(
new
User(
"bbb"
));
list.add(
new
User(
"cccc"
));
test.setDetails(list);
// If the base element is not passed in, Betwixt will guess
// But let's write example bean as base element 'person'
beanWriter.write(
"note"
, test);
// Write to System.out
// (We could have used the empty constructor for BeanWriter
// but this way is more instructive)
System.
out
.println(outputWriter.toString());
// Betwixt writes fragments not documents so does not automatically close
// writers or streams.
// This example will do no more writing so close the writer now.
outputWriter.close();
}
}
最终生成xml:
<?xml version='1.0' ?>
<note>
<detailsaaa>
<useraa>
<subname>aaa</subname>
</useraa>
<useraa>
<subname>bbb</subname>
</useraa>
<useraa>
<subname>cccc</subname>
</useraa>
</detailsaaa>
<note>fdfddf</note>
<title>tesdd</title>
<toWho>fdfdf</toWho>
</note>
<note>
<detailsaaa>
<useraa>
<subname>aaa</subname>
</useraa>
<useraa>
<subname>bbb</subname>
</useraa>
<useraa>
<subname>cccc</subname>
</useraa>
</detailsaaa>
<note>fdfddf</note>
<title>tesdd</title>
<toWho>fdfdf</toWho>
</note>