You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jeff Emminger <ma...@jeffemminger.com> on 2004/09/15 23:46:55 UTC

creating xml from form components

hi list,

just wondering if there's an easier way to do this:

i need to create a string of xml based upon form field's names and 
values, e.g.

  <input name="foo" value="fooValue" />
  <input name="bar" value="barValue" />

would become
  <RATE>
   <foo>fooValue</foo>
   <bar>barValue</bar>
  </RATE>

i have accomplished this so far in the code following - though one 
particular aspect with dates seems too involved, i.e. maybe there's some 
tapestry methods i've missed that would make it easier?

the specific problem i'm talking about is converting a ValidField that 
is a date value validated by a DateValidator - the raw value that i get 
back is in the format "EEE MMM dd hh:mm:ss z yyyy", but the xml needs to 
be in the format "MM/dd/yyyy"

i currently solve this problem by seeing if the component has a 
validator binding, then seeing if that binding is of class 
DateValidator, then running its value through a utility method to 
convert from the existing date pattern to the correct pattern.

any suggestions on a more efficient way to do all this, or do you think 
i've boiled it down already?

thanks for your input,
jeff


the code:

StringBuffer requestXml = new StringBuffer("<RATE>");
Map components = getPage().getComponents();
Set keys = components.keySet();
Iterator i = keys.iterator();

//  loop through the page components
while (i.hasNext()) {
   //  get next component for key at i.next()
   IComponent component =
     (IComponent) components.get((String)i.next());

   IBinding binding = null;
   IBinding validatorBinding = null;

   //  if ValidField or PropertySelection, use raw value
   if (component instanceof ValidField
       || component instanceof PropertySelection) {
     binding = component.getBinding("value");
   }
   //  if RadioGroup, get selected radio's value
   if (component instanceof RadioGroup) {
     binding = ((RadioGroup) component).getSelectedBinding();
   }
   //  only use components with "value" bindings
   if (binding != null) {
     boolean isDate = false;

     /*
      *  if this component has a DateValidator,
      *  we need the date in MM/dd/yyyy format
      */
     validatorBinding = component.getBinding("validator");
     if (validatorBinding != null) {
       Object oValidator = validatorBinding.getObject();
       if (oValidator instanceof DateValidator) {
         isDate = true;
       }
     }
     //  if date field, fix the date format
     if (isDate) {
       requestXml.append("<" + component.getId() + ">");
       requestXml.append(
         Utils.escapeForXml(
           Utils.doFormatDate(
             binding.getString(),
             "EEE MMM dd hh:mm:ss z yyyy",
             "MM/dd/yyyy")
         )
       );
       requestXml.append("</" + component.getId() + ">");
     }
     //  else just use the raw value
     else {
       requestXml.append("<" + component.getId() + ">");
       requestXml.append( Utils.escapeForXml(binding.getString()) );
       requestXml.append("</" + component.getId() + ">");
     }
   }
}
requestXml.append("</RATE>");


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org