<?xml version="1.0" encoding="UTF-8"?>
<root>
<questions>
<!-- 问题库 -->
<question id="1" title="性别" />
<question id="2" title="您的职业" />
<question id="3" title="您属于什么类型" />
<question id="4" title="您属于什么类型" />
</questions>
<!-- 答案库 -->
<answers>
<answer id="1" answer="男" qId="1" />
<answer id="2" answer="女" qId="2" />
<answer id="3" answer="上班族" qId="2" />
<answer id="4" answer="自由职业" qId="2" />
<answer id="5" answer="学生" qId="2" />
<answer id="6" answer="创业者" qId="2" />
<answer id="7" answer="电子发烧友" qId="3" />
<answer id="8" answer="健身达人" qId="3" />
<answer id="9" answer="时尚达人" qId="3" />
<answer id="10" answer="文艺青年" qId="3" />
<answer id="11" answer="美妆达人" qId="4" />
<answer id="12" answer="居家达人" qId="5" />
</answers>
<!-- 答题路线 -->
<path id="1">
<item qId="1" preQId="0" aIds="" />
<item qId="2" preQId="1" aIds="" />
<item qId="3" preQId="2" aIds="1" />
<item qId="4" preQId="2" aIds="2" />
</path>
</root>
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom.JDOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class WechatApp {
public static void main(String[] args) throws JDOMException, IOException {
// List<Map<String, String>> questions = JDomXmlParse(WechatApp.class.getResourceAsStream("/question.xml"), "root");
System.out.println(parseDOMXml(WechatApp.class.getResourceAsStream("/question.xml")));;
// System.out.println(questions);
}
public static List<HashMap<String, String>> parseDOMXml(InputStream xmlStr) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
List<HashMap<String, String>> listMap=new ArrayList<HashMap<String, String>>();
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(xmlStr);
Element root = doc.getDocumentElement();
NodeList rootNodes = root.getChildNodes();
for (int i = 0; i < rootNodes.getLength(); i++) {
Node rootChildNodes = rootNodes.item(i);
if (rootChildNodes != null && rootChildNodes.getNodeType() == Node.ELEMENT_NODE) {
NodeList nodeList = rootChildNodes.getChildNodes();
if (nodeList == null)
continue;
for (int j = 0; j < nodeList.getLength(); j++) {
Node node = nodeList.item(j);
if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
if(rootChildNodes.getNodeName().equals("questions")){
HashMap<String,String> questionsMap=new HashMap<String, String>();
String idk=node.getAttributes().getNamedItem("id").getNodeName().toString();
String idv=node.getAttributes().getNamedItem("id").getNodeValue().toString();
String titlek=node.getAttributes().getNamedItem("title").getNodeName().toString();
String titlev=node.getAttributes().getNamedItem("title").getNodeValue().toString();
questionsMap.put(idk,idv);
questionsMap.put(titlek,titlev);
questionsMap.put("queFlag","queFlag");//添加标识,问题库
listMap.add(questionsMap);
}else if(rootChildNodes.getNodeName().equals("answers")){
HashMap<String,String> answersMap=new HashMap<String, String>();
String idk=node.getAttributes().getNamedItem("id").getNodeName().toString();
String idv=node.getAttributes().getNamedItem("id").getNodeValue().toString();
String answerk=node.getAttributes().getNamedItem("answer").getNodeName().toString();
String answerv=node.getAttributes().getNamedItem("answer").getNodeValue().toString();
String qIdk=node.getAttributes().getNamedItem("qId").getNodeName().toString();
String qIdv=node.getAttributes().getNamedItem("qId").getNodeValue().toString();
answersMap.put(idk, idv);
answersMap.put(answerk, answerv);
answersMap.put(qIdk, qIdv);
answersMap.put("ansFlag", "ansFlag");//添加标识,答案库
listMap.add(answersMap);
}else if(rootChildNodes.getNodeName().equals("path")){
HashMap<String,String> questionnaireMap=new HashMap<String, String>();
String qIdk=node.getAttributes().getNamedItem("qId").getNodeName().toString();
String qIdv=node.getAttributes().getNamedItem("qId").getNodeValue().toString();
String preQIdk=node.getAttributes().getNamedItem("preQId").getNodeName().toString();
String preQIdv=node.getAttributes().getNamedItem("preQId").getNodeValue().toString();
String aIdsk=node.getAttributes().getNamedItem("aIds").getNodeName().toString();
String aIdsv=node.getAttributes().getNamedItem("aIds").getNodeValue().toString();
questionnaireMap.put(qIdk, qIdv);
questionnaireMap.put(preQIdk, preQIdv);
questionnaireMap.put(aIdsk, aIdsv);
questionnaireMap.put("pathFlag", "pathFlag");//添加标识,答题路线
listMap.add(questionnaireMap);
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return listMap;
}
}
打印结果:[{id=1, title=性别, queFlag=queFlag}, {id=2, title=您的职业, queFlag=queFlag}, {id=3, title=您属于什么类型, queFlag=queFlag}, {id=4, title=您属于什么类型, queFlag=queFlag}, {ansFlag=ansFlag, id=1, qId=1, answer=男}, {ansFlag=ansFlag, id=2, qId=2, answer=女}, {ansFlag=ansFlag, id=3, qId=2, answer=上班族}, {ansFlag=ansFlag, id=4, qId=2, answer=自由职业}, {ansFlag=ansFlag, id=5, qId=2, answer=学生}, {ansFlag=ansFlag, id=6, qId=2, answer=创业者}, {ansFlag=ansFlag, id=7, qId=3, answer=电子发烧友}, {ansFlag=ansFlag, id=8, qId=3, answer=健身达人}, {ansFlag=ansFlag, id=9, qId=3, answer=时尚达人}, {ansFlag=ansFlag, id=10, qId=3, answer=文艺青年}, {ansFlag=ansFlag, id=11, qId=4, answer=美妆达人}, {ansFlag=ansFlag, id=12, qId=5, answer=居家达人}, {qId=1, aIds=, preQId=0, pathFlag=pathFlag}, {qId=2, aIds=, preQId=1, pathFlag=pathFlag}, {qId=3, aIds=1, preQId=2, pathFlag=pathFlag}, {qId=4, aIds=2, preQId=2, pathFlag=pathFlag}]