一、需要被解析的xml
解析一个这样的xml:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | <?xml version="1.0" encoding="UTF-8"?><学生花名册>
 <学生 id="1">
 <性别>男</性别>
 <姓名>李华</姓名>
 <年龄>14</年龄>
 <电话>6287555</电话>
 </学生>
 <学生 id="2">
 <性别>女</性别>
 <姓名>张三</姓名>
 <年龄>16</年龄>
 <电话>8273425</电话>
 </学生>
 </学生花名册>
 
 | 
二、利用Dom4j进行解析
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 
 | public void dom4jParseXML(String path){try {
 
 org.dom4j.io.SAXReader reader = new SAXReader();
 
 URL resource = getClass().getClassLoader().getResource(path);
 
 org.dom4j.Document document = reader.read(resource);
 
 org.dom4j.Element rootElement = document.getRootElement();
 
 Iterator it = rootElement.elementIterator();
 
 while (it.hasNext()){
 
 org.dom4j.Element ele = (org.dom4j.Element) it.next();
 
 System.out.println("======开始一个学生的解析======");
 
 List<Attribute> attributes = ele.attributes();
 for (Attribute attribute : attributes) {
 System.out.println(attribute.getName() + ":" + attribute.getValue());
 }
 
 Iterator stdAtt = ele.elementIterator();
 while (stdAtt.hasNext()){
 org.dom4j.Element oneAttr = (org.dom4j.Element) stdAtt.next();
 System.out.println(oneAttr.getQName().getName() +":"+ oneAttr.getText());
 }
 System.out.println("======结束一个学生的解析======");
 
 }
 } catch (DocumentException e) {
 e.printStackTrace();
 }
 }
 
 | 
三、直接利用的jdk的dom进行解析
(目前此段代码还有点儿问题,暂时不究,后期再补)
目标是将上述的xml解析成为一个map对象:
| 12
 3
 4
 5
 6
 7
 8
 
 | [id1:{属性1:值1,
 属性2:值2
 },
 id2:{
 属性1:值1,
 属性2:值2
 }]
 
 | 
代码如下:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 
 | public class XMLParseTest {
 @Test
 public void test(){
 parseXML("students.xml  ");
 }
 private Map<String,Object> parseXML(String path){
 try {
 
 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 DocumentBuilder db = dbf.newDocumentBuilder();
 
 
 
 URL resource = getClass().getClassLoader().getResource(path);
 URLConnection urlConnection = resource.openConnection();
 final InputStream in = urlConnection.getInputStream();
 Document doc = db.parse(in);
 
 
 Element root = doc.getDocumentElement();
 
 
 NodeList childNodes = root.getChildNodes();
 if (null == childNodes){
 return null;
 }
 
 
 Map<String, Object> allStds = new HashMap<String, Object>();
 
 for (int i=0 ; i < childNodes.getLength() ; i++){
 
 Node oneStd = childNodes.item(i);
 
 if (Node.ELEMENT_NODE == oneStd.getNodeType()){
 
 Node idNode = oneStd.getAttributes().getNamedItem("id");
 String id = idNode.getNodeValue();
 
 
 NodeList oneStdChildNodes = oneStd.getChildNodes();
 
 Map<String , Object> oneStdAttrs = new HashMap<String, Object>();
 for (int j=0; j<oneStdChildNodes.getLength(); j++){
 
 Node stdOne = oneStdChildNodes.item(j);
 String attribute = stdOne.getNodeName();
 String val = stdOne.getNodeValue();
 oneStdAttrs.put(attribute,val);
 
 }
 
 allStds.put(id, oneStdChildNodes);
 
 }
 }
 
 return allStds;
 
 } catch (Exception e) {
 e.printStackTrace();
 return null;
 }
 }
 }
 
 
 |