java servlet xml解析
Posted 坏笑的眼镜男
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java servlet xml解析相关的知识,希望对你有一定的参考价值。
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>[email protected]</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解析的主要内容,如果未能解决你的问题,请参考以下文章
Java 模拟servlet执行DTD约束Schema约束dom4j解析(读写xml)
java基础 xml 使用dom4j解析 xml文件 servlet根据pattern 找到class
Servlet[jsp]的Servlet.service()引发了具有根本原因的异常无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/js