Dom4j 解析XML文件拿不到数据,求各位师兄帮忙解答
麻烦各位师兄半忙看下我的解析哪里有问题 我这里取不到值这是我要解析的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<RecNum>428</RecNum>
<PageNum>21</PageNum>
<Node>
<province>山东省</province>
<city>潍坊市</city>
<zone>诸城</zone>
<NodeId>12000500001</NodeId>
<NodeName>洛庄</NodeName>
<WrsDate>2013-05-24 09:59:17</WrsDate>
<RevDate>2013-05-24 09:58:54</RevDate>
<CurSoilWater>57</CurSoilWater>
<AvgSoilWater>57</AvgSoilWater>
<SoilTemp>-1.3</SoilTemp>
<LeafHumi>0</LeafHumi>
<GroundWaterLevel>0</GroundWaterLevel>
<Atmos>99793</Atmos>
<AirTemp>21.6</AirTemp>
<AirHumi>86</AirHumi>
<CurWindSpeed>3.6</CurWindSpeed>
<AvgWindSpeed>4</AvgWindSpeed>
<WindAngle>东北风</WindAngle>
<RainSpeed>0</RainSpeed>
<RainContinue>0</RainContinue>
<RainStartDate>2127-15-31</RainStartDate>
<RainDay>0</RainDay>
<RainMonth>4</RainMonth>
<RainYear>4</RainYear>
<SolarRadiation>448</SolarRadiation>
<Ultraviolet>255</Ultraviolet>
</Node>
<Node>
<province>山东省</province>
<city>潍坊市</city>
<zone>诸城</zone>
<NodeId>12000500004</NodeId>
<NodeName>西园</NodeName>
<WrsDate>2013-05-24 09:55:01</WrsDate>
<RevDate>2013-05-24 09:54:36</RevDate>
<CurSoilWater>44</CurSoilWater>
<AvgSoilWater>44</AvgSoilWater>
<SoilTemp>-1.4</SoilTemp>
<LeafHumi>0</LeafHumi>
<GroundWaterLevel>0</GroundWaterLevel>
<Atmos>100457</Atmos>
<AirTemp>23.5</AirTemp>
<AirHumi>80</AirHumi>
<CurWindSpeed>0.4</CurWindSpeed>
<AvgWindSpeed>1.3</AvgWindSpeed>
<WindAngle>西南风</WindAngle>
<RainSpeed>0</RainSpeed>
<RainContinue>4.4</RainContinue>
<RainStartDate>2013-05-23</RainStartDate>
<RainDay>0</RainDay>
<RainMonth>4.4</RainMonth>
<RainYear>4.4</RainYear>
<SolarRadiation>598</SolarRadiation>
<Ultraviolet>255</Ultraviolet>
</Node>
</root>
/**
* 中路大成抽象实体对象
* @title RealData.java
* @package entity
* @author 杜文俊
* @update 2013-6-7 上午9:19:44
*/
package tobaccoTrans.entity;
import java.io.Serializable;
import java.util.List;
/**
* RealData 历史数据实体对象
* @author 杜文俊
*/
public class HistoryData implements Serializable{
private String RecNum;
private String PageNum;
private String province;
private String city;
private String zone;
private String NodeId;
private String NodeName;
private String WrsDate;
private String RevDate;
private String CurSoilWater;
private String AvgSoilWater;
private String SoilTemp;
private String LeafHumi;
private String GroundWaterLevel;
private String Atmos;
private String AirTemp;
private String AirHumi;
private String CurWindSpeed;
private String AvgWindSpeed;
private String WindAngle;
private String RainSpeed;
private String RainContinue;
private String RainStartDate;
private String RainDay;
private String RainMonth;
private String RainYear;
private String SolarRadiation;
private String Ultraviolet;
//下边是一堆get set 方法
}
package tobaccoTrans.test;DOM XML 解析
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import tobaccoTrans.entity.HistoryData;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Analysis {
public static List getAllHistoryDatas(String path){
List<HistoryData> list = new ArrayList<HistoryData>();
File file = new File(path);
try{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
//获取根节点
Element root = document.getRootElement();
List<Node> nodes = root.selectNodes("/root");
Iterator iter = nodes.iterator();
HistoryData historyDataInfo = new HistoryData();
Element historyDataElement = (Element) iter.next();
String recnum = historyDataElement.elementText("RecNum");
historyDataInfo.setRecNum(historyDataElement.elementText("RecNum"));
historyDataInfo.setPageNum(historyDataElement.elementText("PageNum"));
list.add(historyDataInfo);
List<Node> childNodes = root.selectNodes("/root/Node");
HistoryData childInfo = new HistoryData();
Iterator<Node> childIter = childNodes.iterator();
while(childIter.hasNext()){
Element childElement = (Element) iter.next();
childInfo.setProvince(childElement.elementText("province"));
childInfo.setCity(childElement.elementText("city"));
childInfo.setZone(childElement.elementText("zone"));
childInfo.setNodeId(childElement.elementText("NodeId"));
childInfo.setNodeName(childElement.elementText("NodeName"));
childInfo.setWrsDate(childElement.elementText("WrsDate"));
childInfo.setRevDate(childElement.elementText("RevDate"));
String CurSoilWater = childElement.elementText("CurSoilWater");
childInfo.setCurSoilWater(CurSoilWater);
String AvgSoilWater = childElement.elementText("AvgSoilWater");
childInfo.setAvgSoilWater(AvgSoilWater);
String SoilTemp = childElement.elementText("SoilTemp");
childInfo.setSoilTemp(SoilTemp);
String LeafHumi = childElement.elementText("LeafHumi");
childInfo.setLeafHumi(LeafHumi);
String GroundWaterLevel = childElement.elementText("GroundWaterLevel");
childInfo.setGroundWaterLevel(GroundWaterLevel);
String Atmos = childElement.elementText("Atmos")+"Pa";
childInfo.setAtmos(Atmos);
String AirTemp = childElement.elementText("AirTemp");
childInfo.setAirTemp(AirTemp);
String AirHumi = childElement.elementText("AirHumi");
childInfo.setAirHumi(AirHumi);
String CurWindSpeed = childElement.elementText("CurWindSpeed");
childInfo.setCurWindSpeed(CurWindSpeed);
String AvgWindSpeed = childElement.elementText("AvgWindSpeed");
childInfo.setAvgWindSpeed(AvgWindSpeed);
childInfo.setWindAngle(childElement.elementText("WindAngle"));
String RainSpeed = childElement.elementText("RainSpeed");
childInfo.setRainSpeed(RainSpeed);
String RainContinue = childElement.elementText("RainContinue");
childInfo.setRainContinue(RainContinue);
childInfo.setRainStartDate(childElement.elementText("RainStartDate"));
String RainDay = childElement.elementText("RainDay");
childInfo.setRainDay(RainDay);
String RainMonth = childElement.elementText("RainMonth");
childInfo.setRainMonth(RainMonth);
String RainYear = childElement.elementText("RainYear");
childInfo.setRainYear(RainYear);
String SolarRadiation = childElement.elementText("SolarRadiation");
childInfo.setSolarRadiation(SolarRadiation);
childInfo.setUltraviolet(childElement.elementText("Ultraviolet"));
list.add(childInfo);
}
}catch(Exception e){
e.printStackTrace();
}
return list;
}
/**
* 主方法测试
*/
public static void main(String[] args) {
List list = getAllHistoryDatas("E:\\test.xml");
//循环读取所有xml节点元素
}
}