/**
*
* @param bath 本地xml文件路径
* @return
*/
public Map<String, Object> getXMlData(String bath) {
Map<String, Object> objectMap = new HashMap<String, Object>();
File inputXml = new File(bath);
SAXReader saxReader = new SAXReader();
Document document = null;
try {
//读取xml文件到Document中,得到Document对象
document = saxReader.read(inputXml);
//获取xml文件的根节点
Element root = document.getRootElement();
JSONObject json = new JSONObject();
//获取根节点下节点名为RECORDd
Element RECORD = root.element("RECORD");
Element RESULT = RECORD.element("RESULT");
Element ROW = null;
if(RESULT.element("ROW") != null) {
ROW = RESULT.element("ROW");
List<Element> childElements = ROW.elements();
for(Element child : childElements) {
System.out.println(''节点名称:"+child.getName()+"&&节点内容:"+child.getTextTrim());
json.put(child.getName(), child.getTextTrim());
}
objectMap.put("resultData", json);
objectMap.put("status", 200);
objectMap.put("message", "数据成功");
}else {
objectMap.put("status", 500);
objectMap.put("message", "没有ROW节点");
}
} catch (DocumentException e) {
e.printStackTrace();
objectMap.put("status", 500);
objectMap.put("message", "xml文件路径错误或者系统错误");
}
return objectMap;
}
Dom4j 读取xml文件
最新推荐文章于 2024-09-27 10:08:18 发布