xstream的使用


xstream的使用_第1张图片
 1。為了將上图中的案件参数列表送到后台,我们先将列表中的参数通过javascript拼接成xml或json字符串。如下:

        caseFieldXml+="<caseFields>"
        for(var i=0;i<caseParamNames.length;++i){
          caseFieldXml+="<caseField><fieldName>"+caseParamNames[i].value+"</fieldName>";
          caseFieldXml+="<fieldType>"+caseParamTypes[i].value+"</fieldType>";
          caseFieldXml+="<fieldValue>"+caseParamValues[i].value+"</fieldValue></caseField>";
                var localMessage=validatorData(caseParamTypes[i].value,caseParamValues[i].value,i);
                if(localMessage!="")
                  errorMessage+=localMessage+"\n";
        }
        caseFieldXml+="</caseFields>"

2。其中上面caseField,fieldType,fieldValue分别为FlowCaseField domain类的字段。

 

3。action后台将串过来的xml或json字符串,通过xstream转换成FlowCaseField 的list对象。后台代码如下:

 private XStream getXStream() {
  if (xStream == null) {
   XStream LocalXStream = new XStream(new DomDriver());
   LocalXStream.alias("caseFields", List.class);
   LocalXStream.alias("caseField", FlowCaseField.class);
   return LocalXStream;
  }
  return xStream;
 }

若要将json串转换成FlowCaseField 的list对象,则使用 XStream LocalXStream = new XStream(new JsonHierarchicalStreamDriver());

4.最后使用XStream对象的fromXML方法将xml串转换成FlowCaseField 的list对象,代码如下:

List<FlowCaseField> flowCaseFields = (List) getXStream().fromXML(caseFieldXml);

你可能感兴趣的:(JavaScript,xml,json)