java使用dom4j解析xml并展示demo(科帮网)
java使用dom4j解析xml并展示demo(科帮网):stuName.xml:<?xml version="1.0" encoding="UTF-8"?>
<result>
<question>
<code>001</code>
<name>张三</name>
</question>
<question>
<code>002</code>
<name>李四</name>
</question>
<question>
<code>003</code>
<name>王二</name>
</question>
<question>
<code>004</code>
<name>麻子</name>
</question>
<question>
<code>005</code>
<name>科帮网</name>
</question>
</result>XmlReadUtil.java:读取方法
package com.itstyle.util;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
*
* @author 科帮网(www.52itstyle.com)
*
*/
public class XmlReadUtil {
@SuppressWarnings("rawtypes")
public static void listBaseInfo(String filePath, Map<String, String> map) {
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(new File(filePath));
Element root = document.getRootElement();
// 用于记录信息编号的变量
int num = -1;
// 遍历根结点(result)的所有孩子节点(肯定是question节点)
Attribute attrCode = root.attribute("code");
Attribute attrName = root.attribute("name");
if(attrCode!=null){
map.put(root.getName()+"-"+attrCode.getName(), attrCode.getValue());
}
if(attrName!=null){
map.put(root.getName()+"-"+attrName.getName(), attrName.getValue());
}
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next();
num++;
// 遍历question结点的所有孩子节点(即code, name),并进行处理
for (Iterator iterInner = element.elementIterator(); iterInner
.hasNext();) {
Element elementInner = (Element) iterInner.next();
map.put(elementInner.getName() + num,
elementInner.getText());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
ReadXmlServlet.java:
package com.itstyle.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.itstyle.model.CommonEntity;
import com.itstyle.util.PropertiesListUtil;
/**
*
* @author 科帮网(www.52itstyle.com)
*
*/
public class ReadXmlServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private List<CommonEntity> listStu = new ArrayList<CommonEntity>();
public void init() throws ServletException {
// 初始化阶段xml
String path = getServletContext().getRealPath("/file/stuName.xml");
try {
listStu = PropertiesListUtil.listXml(path);
System.out.println(listStu.size());
} catch (Exception e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
JSONArray array = new JSONArray();
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
try {
for(CommonEntity entity:listStu){
JSONObject obj = new JSONObject();
obj.put("code", entity.getEntityCode());
obj.put("name", entity.getEntityName());
array.add(obj);
}
out.print(array.toString());
}catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
java使用dom4j解析xml并展示demo(科帮网)下载:点击下载
下来,学习一下。谢谢:victory: 这个项目太棒勒!下下来学习下!
页:
[1]