当有一对多的关系时,生成xml:
package betwixt;
import java.util.ArrayList;
import java.util.List;
publicclass NoteBean {
private String toWho;
private String fromWho;
private String title;
private String note;
private List<User> details=new ArrayList <User>();
…….
publicclass User {
private String name;
private String password;
/**
*@returnthename
*/
public String getName() {
returnname;
}
/**
*@paramnamethenametoset
*/
publicvoid setName(String name) {
this.name = name;
}
public User(String name) {
this.name = name;
}
/**
*@returnthepassword
*/
public String getPassword() {
returnpassword;
}
/**
*@parampasswordthepasswordtoset
*/
publicvoid 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>
测试:
publicclass WriteExampleApp {
/**
*Createanexamplebeanandthenconvertittoxml.
*/
publicstaticfinalvoid 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>