java dom4j 读取xml, 将xml 转换成 List<map<?>>?

想把一个xml 读取出来,并装入 list 集合中。
想要的格式 [map1, map2]
xml 格式如下
<?xml version="1.0" encoding="UTF-8"?>
<testerInterface>
<interface name="login" run="Y">
<url>
http://192.168.1.78
</url>
<testDataPath> mobileApiCase.xlsx </testDataPath>
<exceleSheetName> Login </exceleSheetName>
</interface>
<interface name="Help" run="n">
<url>tesss</url>
<testDataPath> ./mobileApiCase.xlsx </testDataPath>
<exceleSheetName> Help </exceleSheetName>
</interface>
</testerInterface>

大概是这样 xml 中有多个 interface 节点, 每个interface 下的所有子节点的名称 和 value 放到一个map中, 然后再将 map 放到 list中, 请大神帮帮忙
最新回答
跨步大

2024-09-18 07:24:55

String truepathSub = truepath.substring(0, truepath.length()-4);
String truepath1Sub = zipnpath.substring(0, zipnpath.length()-4);
String zipname = truepath.substring(25, truepath.length()-4);
SAXReader saxRead = new SAXReader();
Document inDoc = saxRead.read(new File("C:\\apache-tomcat-6.0.29\\webapps\\xml\\"+truepathSub+
"\\"+truepath1Sub+"\\Test MetaData.xml"));

Element rootEl = inDoc.getRootElement();
Element element = rootEl.element("Datas");
//Element element1 = element.element("TestData");

for(Iterator i = element.elementIterator();i.hasNext();){

Element bookEl = (Element) i.next();

String Identifier = bookEl.elementText("Identifier");
String Type = bookEl.elementText("Type");
String Questions = bookEl.elementText("Questions");
String Difficulty = bookEl.elementText("Difficulty");
String SpecialtyType = bookEl.elementText("SpecialtyType");
String Specialty = bookEl.elementText("Specialty");
String Keyword = bookEl.elementText("Keyword");
String Answer = bookEl.elementText("Answer");
String Analysis = bookEl.elementText("Analysis");
String Contribute = bookEl.elementText("Contribute");
String Entity = bookEl.elementText("Entity");
String Date = bookEl.elementText("Date");
String Region = bookEl.elementText("Region");
String TypeOfSchool = bookEl.elementText("TypeOfSchool");
String EducationLevels = bookEl.elementText("EducationLevels");
String Course = bookEl.elementText("Course");

List<Stysj> bookList = new ArrayList<Stysj>();
Stysj stysj = null;
stysj = new Stysj();
stysj.setIdentifier(Identifier);
stysj.setType(Type);
stysj.setQuestions(Questions);
stysj.setDifficulty(Difficulty);
stysj.setSpecialtyType(SpecialtyType);
stysj.setSpecialty(Specialty);
stysj.setKeyword(Keyword);
stysj.setAnswer(Answer);
stysj.setAnalysis(Analysis);
stysj.setContribute(Contribute);
stysj.setEntity(Entity);
stysj.setDate(Date);
stysj.setRegion(Region);
stysj.setTypeOfSchool(TypeOfSchool);
stysj.setEducationLevels(EducationLevels);
stysj.setCourse(Course);
stysj.setXmlname(zipname);
bookList.add(stysj);
simpleService.save(stysj);