views:

129

answers:

2

How do I map "Joe Smith" to first name "Joe" last name "Smith"?

I already have code to split up the name, I'm not sure how to make that work with the Digester though.

<guestlist>
  <guest>
   <name>Joe Smith</name>
  </guest>
</guestlist>

public class Guest(){
  private String firstName;
  private String lastName;
...
}
A: 
// 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.
}

Note: SplitFunc_FirstName is your custom function which you already wrote to extact first and last name.

Rasik Jain
This is neither in Java nor using Commons Digester.
ChssPly76
Hi ChssPly76, This is in C# 3.0 using LinqToXML and Anonymous Types.
Rasik Jain
Did not noticed the "commons digester", that was the reason for C# solution.
Rasik Jain
+1  A: 

An easy answer is: add an additional property to your Guest class:

public class Guest {
 private String firstName;
 private String lastName;
 public void setBothNames(String bothNames) {
  String[] split = bothNames.split(" ");
  firstName = split[0];
  lastName = split[1];
 }

and the bean property setter rule to the digester:

 digester.addBeanPropertySetter("guestlist/guest/name", "bothNames");
mhaller
This would work, but It's not ideal. I'm looking for more of a plugin or converter or something.
ScArcher2