dom4j使用
--------------------编程问答--------------------
把这个xml文件与这个类在相同的目录下,java代码如下:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.util.*;
public class Dom4jTest1
{
public static void main(String[] args) throws Exception
{
SAXReader reader = new SAXReader();
Document doc = reader.read(Dom4jTest1.class
.getResourceAsStream("student.xml"));
Element root = doc.getRootElement();
List<Element> students = root.elements();
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (Element student : students)
{
String sn = student.attributeValue("sn");
Element name = student.element("name");
Element age = student.element("age");
Map<String, String> map = new HashMap<String, String>();
map.put("sn", sn);
map.put("name", name.getTextTrim());
map.put("age", age.getTextTrim());
list.add(map);
}
}
}
补充:Java , Java相关