码迷,mamicode.com
首页 > 编程语言 > 详细

java servlet xml解析

时间:2017-03-21 10:59:25      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:size   protected   添加   put   encoding   研究院   ide   失败   pos   

xml文件格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<operationXml>
<trans>
<versionId>8a8a93b05ad07ded015aea9ac3480416</versionId>
<transType>ADD_USER</transType>
<transXml>
<id>8a8a93b05ad07ded015aea9ac3390413</id>
<userId>syn_michelle_xi_91990415</userId>
<appId>4028fb83507dd41701507dd614ec0003</appId>
<display>0</display>
<username>syn_michelle_xi_91990415</username>
<pwd>8a8a93b05ad07ded015aea9ac3390414</pwd>
<organId>O0000000000000025622</organId>
<organCode>8a8a93b05ad07ded015aea62f80b03c9</organCode>
<struId>S0000000000000037508</struId>
<struPath>rootId#1#S0000000000000000424#S0000000000000037508</struPath>
<parentOrganId>O0000000000000000436</parentOrganId><
parentOrganCode>0000000000110100000000466000000000002</parentOrganCode>
<name>王熙</name>
<organ_mobile>18600092536</organ_mobile>
<organ_email>xiwang19820919@163.com</organ_email>
<zjlx>0</zjlx><zjhm>360102198209190025</zjhm>
<rid></rid>
<unitname>中国民航科学技术研究院</unitname>
<unitbsdtid>D77B02867A594208AABE1C41E38B8A47</unitbsdtid>
<zzjgdm>400007439</zzjgdm>

<surveyleveltype>0</surveyleveltype>
<tyshxydm>121000004000074395</tyshxydm>
<prefecture>100000</prefecture>
<sfdlxxqy></sfdlxxqy>
</transXml>
</trans></operationXml>

 

java方法:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

Map<String,String> map = new HashMap<String,String>();
JSONObject json = null;
List<User> saveUserList = new ArrayList<User>();
//String versionId = request.getParameter("versionId");
//String action = request.getParameter("action");
String versionId = "8a8a93b05ad07ded015aea9ac3480416";
String action = "get_syn";

// 初始化,该类为 用于对数据库操作
PostgisDAO pdao = new PostgisDAO();
pdao.initPostDB();

SynchronizeClient sc = new SynchronizeClient (UrlString);
System.out.println("action:" + action);// 此为通知中获取的action
System.out.println("versionId:" + versionId); // 此为通知中获取的versionId

String dataXml = sc.getTransXmlByVersionId(action, versionId);

if(!UtilMethod.isEmpty(dataXml)){
try {
Document document = DocumentHelper.parseText(dataXml);
Element rootEle = document.getRootElement();
Element trans = rootEle.element("trans");
//获取标识 参数
String backVersionId = trans.elementText("versionId");
String transType = trans.elementText("transType");

//获取用户信息
@SuppressWarnings("unchecked")

//这里的transXml标签可能有多个 所以用elements

List<Element> transXmlList = trans.elements("transXml");

for(Element transXml : transXmlList){

String id = transXml.elementText("id");
String userId = transXml.elementText("userId");
String appId = transXml.elementText("appId");
String display = transXml.elementText("display");
String username = transXml.elementText("username");
String pwd = transXml.elementText("pwd");
String organId = transXml.elementText("organId");
String organCode = transXml.elementText("organCode");
String struId = transXml.elementText("struId");
String struPath = transXml.elementText("struPath");
String parentOrganId = transXml.elementText("parentOrganId");
String parentOrganCode = transXml.elementText("parentOrganCode");
String name = transXml.elementText("name");
String organ_mobile = transXml.elementText("organ_mobile");
String organ_email = transXml.elementText("organ_email");
String zjlx = transXml.elementText("zjlx");
String zjhm = transXml.elementText("zjhm");
String rid = transXml.elementText("rid");
String unitname = transXml.elementText("unitname");
String unitbsdtid = transXml.elementText("unitbsdtid");
String zzjgdm = transXml.elementText("zzjgdm");
String surveyleveltype = transXml.elementText("surveyleveltype");
String tyshxydm = transXml.elementText("tyshxydm");
String prefecture = transXml.elementText("prefecture");
String sfdlxxqy = transXml.elementText("sfdlxxqy");

List<User> list_user = pdao.selectUserById(userId);
if(list_user.size() == 0){
User user = new User();
user.setUserName(userId);
user.setUserPassword(pwd);
user.setUserPhone(Long.parseLong(organ_mobile));
user.setUserMail(organ_email);
saveUserList.add(user);
}

}
//如果本系统中已经存在这该用户,那么 它不会添加到saveUserList中(即本系统中的不变)
Boolean result = pdao.saveUser(saveUserList);
if(result){
map.put("message", "同步成功");
map.put("type", "1");
}else{
map.put("message", "同步失败");
map.put("type", "0");
}
json = JSONObject.fromMap(map);

} catch (DocumentException | SQLException e) {
e.printStackTrace();
log.error("同步数据失败,新增本地用户失败", e);

}
}
// 设置响应类型
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(json.toString());

}

java servlet xml解析

标签:size   protected   添加   put   encoding   研究院   ide   失败   pos   

原文地址:http://www.cnblogs.com/woaiadu/p/6592779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!