如何将“Joe Smith”映射到名字“Joe”姓氏“Smith”?
我已经有了拆分名称的代码,但我不确定如何使用 Digester 进行操作。
<guestlist>
<guest>
<name>Joe Smith</name>
</guest>
</guestlist>
public class Guest(){
private String firstName;
private String lastName;
...
}
如何将“Joe Smith”映射到名字“Joe”姓氏“Smith”?
我已经有了拆分名称的代码,但我不确定如何使用 Digester 进行操作。
<guestlist>
<guest>
<name>Joe Smith</name>
</guest>
</guestlist>
public class Guest(){
private String firstName;
private String lastName;
...
}
一个简单的答案是:向您的 Guest 类添加一个附加属性:
public class Guest {
private String firstName;
private String lastName;
public void setBothNames(String bothNames) {
String[] split = bothNames.split(" ");
firstName = split[0];
lastName = split[1];
}
以及消化器的 bean 属性设置器规则:
digester.addBeanPropertySetter("guestlist/guest/name", "bothNames");
// Loading from a file, you can also load from a stream
XDocument loaded = XDocument.Load(@"C:\Guests.xml");
// Query the data and write out a subset of guests
var guests= from c in loaded.Descendants("guest")
select new
{
FirstName = SplitFunc_FirstName(c.Element("name")),
LastName = SplitFunc_LastName(c.Element("name"))
};
foreach (var guest in guests)
{
Your custom code...to attach it to your entity object.
}
注意:SplitFunc_FirstName 是您已经编写的用于提取名字和姓氏的自定义函数。