首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

Dom4j 解析XML资料拿不到数据,求各位师兄帮忙解答

2013-07-04 
Dom4j 解析XML文件拿不到数据,求各位师兄帮忙解答麻烦各位师兄半忙看下我的解析哪里有问题 我这里取不到值

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;

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节点元素

}
}

DOM XML 解析
[解决办法]
分开来做:先保证 Dom4j取到结点的内容,然后再组装成对象。
[解决办法]
String province= root.getChild ( "root" ).getChildText ( "province" );
注意层次对不对,应该是根元素孩子的孩子
[解决办法]
引用:
Quote: 引用:


<?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>

你的文件编码不对,试过了,这个可以

这个编码跟我的有什么不一样吗

你的那个用浏览器打开就会报错,到那个汉子开始的地方,
浏览器报错:
无法显示 XML 页。 
使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。 
文本内容中发现无效字符。处理资源 'file:///H:/test.xml' 时出错。第 6 行,位置: 29 

eclipse报错:
1 字节的 UTF-8 序列的字节 1 无效。 Nested exception: 1 字节的 UTF-8 序列的字节 1 无效。

说明你的文件本身就不正确:
我的这个是在eclipse下新建的文件再拷贝的,运行了可以,你可以拷我的试一下看可不可以。

热点排行