java 返回xml jquery处理
import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import com.autotoll.jv.erp.proxy.PortletActionBeanProxy;import com.liferay.portal.struts.AJAXAction;import com.liferay.portal.theme.ThemeDisplay;import com.liferay.portal.util.WebKeys;import com.autotoll.jv.pojo.impl.Citytable;public class ShowCityAjax extends AJAXAction{public String getText(ActionMapping arg0, ActionForm arg1,HttpServletRequest request, HttpServletResponse response) throws Exception {ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);String language = themeDisplay.getLanguageId();String pid = request.getParameter("pid");String cityId = "";String cityName = "";Document document = DocumentHelper.createDocument();Element root = document.addElement("city");if (pid !=null && ! pid.trim().equals("")) {List<Citytable> list = PortletActionBeanProxy.getInstance().getCityTableSvc().findCityByPId(Long.parseLong(pid));if (list !=null && list.size()>0) {for (Citytable city :list) {cityId = city.getCityid().toString();if(language.trim().equals("zh_CN")) {cityName = city.getCitynameSc();} else if(language.trim().equals("zh_TW")) {cityName = city.getCitynameTc();} else {cityName = city.getCitynameEng();}Element cId = root.addElement("cityId");Element cName = root.addElement("cityName");cId.addText(cityId);cName.addText(cityName);}}}//System.out.println(root.asXML());return root.asXML();}jsp 页面:
function toXML(strxml){ try{ xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.loadXML(strxml); } catch(e){ var oParser=new DOMParser(); xmlDoc=oParser.parseFromString(strxml,"text/xml"); } return xmlDoc; } function getCity(obj){var pid = obj.options.value;if (pid != "") {jQuery.ajax({url: '<%= themeDisplay.getPathMain() %>/erp/showCity', data: {pid:pid}, success: setOptions});} else {var selectObj = document.getElementById('city_');selectObj.options.length = 0;selectObj.options.add(new Option('<liferay-ui:message key="erp.select.option.choose"/>',""));document.getElementById("address-alert-span").style.display = "";}}function setOptions(xml) {var radioValue = getRadioValue();var selectObj = document.getElementById('city_');selectObj.options.length = 0;var xmlobj = toXML(xml);var cidArr = xmlobj.getElementsByTagName('city/cityId');var cNameArr = xmlobj.getElementsByTagName('city/cityName');selectObj.options.add(new Option('<liferay-ui:message key="erp.select.option.choose"/>',""));for(var i=0; i<cidArr.length; i++){selectObj.options.add(new Option(cNameArr.childNodes(0).nodeValue,cidArr.childNodes(0).nodeValue));}document.getElementById('city').value=cidArr.childNodes(0).nodeValue;if (document.getElementById('city').value != "" && document.getElementById('address').value != "") { document.getElementById("address-alert-span").style.display = "none"; } else { document.getElementById("address-alert-span").style.display = ""; }}
页:
[1]