使用Dmo4j解析xml总结

前端之家收集整理的这篇文章主要介绍了使用Dmo4j解析xml总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

dom4j是用的比较多的解析xml的插件,使用起来也是比较简单灵活的。dom4j需要的jar包,仅仅需要dom4j.jar。本例中用的1.6.1的版本,下载地址:http://download.csdn.net/detail/qq_33556185/9601807

已知xml结构如下:

<?xml version="1.0" encoding="UTF-8"?>
<Level>
	<level1 id="level1">
		<grade>1</grade>
		<expnumber>10</expnumber>
		<expname>V1</expname>
	</level1>
	<level2 id="level2">
		<grade>2</grade>
		<expnumber>20</expnumber>
		<expname>V2</expname>
	</level2>
	<level3 id="level3">
		<grade>3</grade>
		<expnumber>30</expnumber>
		<expname>V3</expname>
	</level3>
	<level4 id="level4">
		<grade>4</grade>
		<expnumber>40</expnumber>
		<expname>V4</expname>
	</level4>
	<level5 id="level5">
		<grade>5</grade>
		<expnumber>50</expnumber>
		<expname>V5</expname>
	</level5>
	<level6 id="level6">
		<grade>6</grade>
		<expnumber>60</expnumber>
		<expname>V6</expname>
	</level6>
	<level7 id="level7">
		<grade>7</grade>
		<expnumber>70</expnumber>
		<expname>V7</expname>
	</level7>
	<level8 id="level8">
		<grade>8</grade>
		<expnumber>80</expnumber>
		<expname>V8</expname>
	</level8>
	<level9 id="level9">
		<grade>9</grade>
		<expnumber>90</expnumber>
		<expname>V9</expname>
	</level9>
	<level10 id="level10">
		<grade>10</grade>
		<expnumber>100</expnumber>
		<expname>V10</expname>
	</level10>
	<level11 id="level11">
		<grade>11</grade>
		<expnumber>120</expnumber>
		<expname>V11</expname>
	</level11>
	<level12 id="level12">
		<grade>12</grade>
		<expnumber>140</expnumber>
		<expname>V12</expname>
	</level12>
	<level13 id="level13">
		<grade>13</grade>
		<expnumber>160</expnumber>
		<expname>V13</expname>
	</level13>
	<level14 id="level14">
		<grade>14</grade>
		<expnumber>180</expnumber>
		<expname>V14</expname>
	</level14>
	<level15 id="level15">
		<grade>15</grade>
		<expnumber>200</expnumber>
		<expname>V15</expname>
	</level15>
	<level16 id="level16">
		<grade>16</grade>
		<expnumber>220</expnumber>
		<expname>V16</expname>
	</level16>
	<level17 id="level17">
		<grade>17</grade>
		<expnumber>240</expnumber>
		<expname>V17</expname>
	</level17>
	<level18 id="level18">
		<grade>18</grade>
		<expnumber>260</expnumber>
		<expname>V18</expname>
	</level18>
	<level19 id="level19">
		<grade>19</grade>
		<expnumber>300</expnumber>
		<expname>V19</expname>
	</level19>	
	<level20 id="level20">
		<grade>20</grade>
		<expnumber>340</expnumber>
		<expname>V20</expname>
	</level20>
	<level21 id="level21">
		<grade>21</grade>
		<expnumber>380</expnumber>
		<expname>V21</expname>
	</level21>
	<level22 id="level22">
		<grade>22</grade>
		<expnumber>420</expnumber>
		<expname>V22</expname>
	</level22>
	<level23 id="level23">
		<grade>23</grade>
		<expnumber>460</expnumber>
		<expname>V23</expname>
	</level23>
	<level24 id="level24">
		<grade>24</grade>
		<expnumber>500</expnumber>
		<expname>V24</expname>
	</level24>
	<level25 id="level25">
		<grade>25</grade>
		<expnumber>600</expnumber>
		<expname>V25</expname>
	</level25>
	<level26 id="level26">
		<grade>26</grade>
		<expnumber>700</expnumber>
		<expname>V26</expname>
	</level26>
	<level27 id="level27">
		<grade>27</grade>
		<expnumber>800</expnumber>
		<expname>V27</expname>
	</level27>
	<level28 id="level28">
		<grade>28</grade>
		<expnumber>900</expnumber>
		<expname>V28</expname>
	</level28>
	<level29 id="level29">
		<grade>29</grade>
		<expnumber>1400</expnumber>
		<expname>V29</expname>
	</level29>
	<level30 id="level30">
		<grade>30</grade>
		<expnumber>1900</expnumber>
		<expname>V30</expname>
	</level30>
	<level31 id="level31">
		<grade>31</grade>
		<expnumber>3000</expnumber>
		<expname>V31</expname>
	</level31>
	<level32 id="level32">
		<grade>32</grade>
		<expnumber>5000</expnumber>
		<expname>V32</expname>
	</level32>
	<level33 id="level33">
		<grade>33</grade>
		<expnumber>10000</expnumber>
		<expname>V33</expname>
	</level33>
</Level>  
解析代码如下:
package com.mvc;

import java.io.File;  
import java.util.ArrayList;
import java.util.List;  
import org.dom4j.Document;  
import org.dom4j.Element;  
import org.dom4j.io.SAXReader;  
import org.junit.Test;  
import com.core.BaseJunitTest;
import com.mvc.vo.LevelVo;
public class Dom4jTest extends BaseJunitTest{
     private List<LevelVo> levelList=new ArrayList<LevelVo>();
     @Test  
     public void test() throws Exception {  
        // 创建saxReader对象  
        SAXReader reader = new SAXReader();  
        // 通过read方法读取一个文件 转换成Document对象  
        Document document = reader.read(new File("resource/level.xml"));  
        //获取根节点元素对象  
        Element root = document.getRootElement();  
        List<Element> childElements = root.elements();
        System.out.println("childElements的节点数是============="+childElements.size());
        List<LevelVo> levelList=new ArrayList<LevelVo>();
        //已知属性名情况下
        for (Element child : childElements) {
        	 System.out.println("id: " + child.attributeValue("id"));
        	 List<Element> grandSonElements = child.elements();
        	 LevelVo levelVo=new LevelVo();
        	 for (Element grandSon : grandSonElements) {
        		 if(grandSon.getQualifiedName().equals("grade")){
        			 levelVo.setGrade(grandSon.getData().toString());
        		 }
        		 if(grandSon.getQualifiedName().equals("expnumber")){
        			 levelVo.setExpnumber(Long.parseLong(grandSon.getData().toString()));
        		 }
        		 if(grandSon.getQualifiedName().equals("expname")){
        			 levelVo.setExpname(grandSon.getData().toString());
        		 }
			}
        	 levelList.add(levelVo);
        }
    }  
  
    public List<LevelVo> getLevelList() {
		return levelList;
    }
    public void setLevelList(List<LevelVo> levelList) {
		this.levelList = levelList;
    }  
    
}
原文链接:https://www.f2er.com/xml/294915.html

猜你在找的XML相关文章