System.out.println("==================进入testApiXml()==================");
//localhost:8080/scms_sin/login/Login/testApi.jspx
String method = ServletActionContext.getRequest().getMethod();
System.out.println(method);
String ret = "";
String xtbh = request.getParameter("xtbh");
String jkxlh = request.getParameter("jkxlh");
String jkid = request.getParameter("jkid");
try {
StringBuffer info = new java.lang.StringBuffer();
InputStream in = request.getInputStream();
BufferedInputStream buf = new BufferedInputStream(in);
byte[] buffer = new byte[1024];
int iRead;
while ((iRead = buf.read(buffer)) != -1) {
info.append(new String(buffer, 0, iRead, "UTF-8"));
}
// 创建saxReader对象
SAXReader reader = new SAXReader();
// 通过read方法读取一个文件 转换成Document对象
Document document = DocumentHelper.parseText(info.toString());
//获取根节点元素对象
Element node = document.getRootElement();
System.out.println("根节点:" + node.getName()); // 拿到根节点的名称
//遍历所有的元素节点
Iterator iter = node.elementIterator("cxtj"); // 获取根节点下的子节点cxtj
String yhm = "";
String dlmm = "";
while (iter.hasNext()) {
Element cxtj = (Element) iter.next();
yhm = cxtj.elementTextTrim("yhm"); // 拿到cxtj节点下的子节点yhm值
dlmm = cxtj.elementTextTrim("dlmm"); // 拿到cxtj节点下的子节点yhm值
System.out.println("yhm:"+yhm+";dlmm:"+ dlmm);
}
if(StringUtils.isBlank(yhm)){
ret=this.createErrorMsgs("0", "非法的查询条件【用户名为空】");
return null;
}
if(StringUtils.isBlank(dlmm)){
ret=this.createErrorMsgs("0", "非法的查询条件【登录密码为空】");
return null;
}
//查询用户是否存在
Map<String, Object> map = new HashMap<String, Object>();
map.put("loginName", yhm);
baseUser=baseLoginManager.checkUser(map);
if(baseUser == null){
baseUser = new BaseUser();
}
if(StringUtils.isNotBlank(baseUser.getLoginName())){
//查询数据是否存在
baseUser=baseLoginManager.login(yhm, MD5.getMd5(dlmm));
if(baseUser == null){
baseUser = new BaseUser();
}
if(StringUtils.isNotBlank(baseUser.getLoginName())){
ret=this.createErrorMsgs("1", "用户存在");
}else{
ret=this.createErrorMsgs("0", "【密码有误】");
}
}else{
ret=this.createErrorMsgs("0", "【用户不存在】");
}
/*//创建xml
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("root");
//生成root的一个接点
Element category = root.addElement("user");
//生产user的一个接点
Element id = category.addElement("id"); */
response.setContentType("text/xml;charset=utf-8");
PrintWriter out = null;
out = response.getWriter();
out.print(ret);
} catch (Exception e) {
e.printStackTrace();
ret=this.createErrorMsgs("0", "调用接口异常:"+e.getMessage());
}
//System.out.println(get);
java实现接受,并且通过dom4j解析xml
最新推荐文章于 2024-07-18 14:43:20 发布