@ResponseBody
@RequestMapping("/getXmlNodes")
public List<ZTreeNode> getXmlNodes(Long versionId){
try {
File file = objectFileService.getFileByVersionId(versionId);
String s;
InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader reader = new BufferedReader(in);
StringBuffer content = new StringBuffer();
while ((s = reader.readLine()) != null) {
content = content.append(s + "\r\n");
}
if (null != reader) {
reader.close();
}
JSONObject jsonObject = XMLUtil.xml2JSONObject(content.toString());
List<ZTreeNode> zTreeNodeList=new ArrayList<>();
Integer parent = null;
test(jsonObject,1, parent, zTreeNodeList, null);
return zTreeNodeList;
}catch (Exception e){
System.out.println(e.getMessage());
}
return null;
}
public void test(Object o,Integer index, Integer parent, List<ZTreeNode> nodes, String xkey) {
if (o instanceof JSONObject) {
Iterator<String> it = ((JSONObject) o).keySet().iterator();
while (it.hasNext()) {
String key = it.next();
if (((JSONObject) o).get(key) instanceof JSONArray
|| ((JSONObject) o).get(key) instanceof JSONObject) {
int nodeId=nodes.size() + 1;
if (!(((JSONObject) o).get(key) instanceof JSONArray)) {
nodes.add(new ZTreeNode(String.valueOf(nodeId), String.valueOf(parent), key));
parent = nodeId;
}
test(((JSONObject) o).get(key),nodeId, parent, nodes, ((JSONObject) o).get(key) instanceof JSONArray ? key : null);
}else {
Map<String, Object> map = nodes.get(index-1).getAttrMap();
if(map==null){
map=new HashMap<>();
}
map.put(key,((JSONObject) o).get(key));
nodes.get(index-1).setAttrMap(map);
}
}
} else if (o instanceof JSONArray) {
JSONArray ja = (JSONArray) o;
for (Object obj : ja) {
int nodeId = nodes.size() + 1;
nodes.add(new ZTreeNode(String.valueOf(nodeId), String.valueOf(parent), xkey));
test(obj,nodeId, nodeId, nodes, xkey);
}
}
}
上述代码有漏洞,看最新的博客文章(修复漏洞):