使用dom4j解析和保存xml时,为什么会解析出错,多写一些字符进去?

stefli 2007-12-10 11:07:21
用Dom4j是要好些,不过又有问题出现了,就是总是解析的时候会多存字符进去,导致xml格式错误。
比如

<?xml version="1.0" encoding="utf-8"?>
<dataList>
<data><item>test</item></data>
</dataList>


总是出现
<?xml version="1.0" encoding="utf-8"?>
<dataList>
<data><item>test</item></data>
</dataList>>


或者

[code=XML]]<?xml version="1.0" encoding="utf-8"?>
<dataList>
<data><item>test</item></data>
</dataList>dataList>
[/code


就是最后总会多出一些数据来。
...全文
273 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
stefli 2007-12-10
  • 打赏
  • 举报
回复

<?xml version="1.0" encoding="utf-8"?>
<dataList>
<data>
<item1>24.41</item1>
<item2>12.59</item2>
<item3>6.2</item3>
<item4>10</item4>
<item5>304</item5>

<item6>33</item6>
<item7>0.00</item7>
<item8>128</item8>
<item9>69.9</item9>
<item10>1.9</item10>

<item11>开机</item11>
<item12>26</item12>
<item13>128</item13>
<item21>停机</item21>
<item22>26</item22>
<item23>128</item23>
<item31>开机</item31>
<item32>26</item32>
<item33>128</item33>
<item41>-</item41>
<item42>26</item42>
<item43>128</item43>

<item51>-</item51>
<item52>26</item52>
<item53>128</item53>
<item61>-</item61>
<item62>26</item62>
<item63>128</item63>
<item71>-</item71>
<item72>26</item72>
<item73>128</item73>
<item81>-</item81>
<item82>26</item82>
<item83>128</item83>

<temperature>23.9</temperature>
<humidity>99.9</humidity>

<item91>2.41</item91>
<item92>2.59</item92>
<item93>0</item93>
<item94>0</item94>
<item95>4</item95>
<item96>3</item96>
<item97>0.00</item97>
<item98>8</item98>
<item99>0.9</item99>
<item910>4.41</item910>
<item911>2.41</item911>
<item912>2.59</item912>
<item913>6.82</item913>
<item914>0</item914>
<item915>4</item915>
<item916>3</item916>

<item101>6.9</item101>
<item102>9.9</item102>
<item111>0.9</item111>
<item112>9.9</item112>
</data>
</dataList>
stefli 2007-12-10
  • 打赏
  • 举报
回复

IService service = (IService) this
.getBean("service");
FileInputStream fi = null;
String url = servlet.getServletContext().getRealPath(File.separator)
+ "/data.xml";
SAXReader saxReader = new SAXReader();
fi = new FileInputStream(url);
Document doc = saxReader.read(fi);
Node item1 = doc.selectSingleNode("/dataList/data/item1");
Node item2 = doc.selectSingleNode("/dataList/data/item2");
Node item3 = doc.selectSingleNode("/dataList/data/item3");
Node item4 = doc.selectSingleNode("/dataList/data/item4");
Node item5 = doc.selectSingleNode("/dataList/data/item5");
Node item6 = doc.selectSingleNode("/dataList/data/item6");
Node item7 = doc.selectSingleNode("/dataList/data/item7");
Node item8 = doc.selectSingleNode("/dataList/data/item8");
Node item9 = doc.selectSingleNode("/dataList/data/item9");
Node item10 = doc.selectSingleNode("/dataList/data/item10");
Node item11 = doc.selectSingleNode("/dataList/data/item11");
Node item12 = doc.selectSingleNode("/dataList/data/item12");
Node item13 = doc.selectSingleNode("/dataList/data/item10");
Node item21 = doc.selectSingleNode("/dataList/data/item21");
Node item22 = doc.selectSingleNode("/dataList/data/item22");
Node item23 = doc.selectSingleNode("/dataList/data/item23");
Node item31 = doc.selectSingleNode("/dataList/data/item31");
Node item32 = doc.selectSingleNode("/dataList/data/item32");
Node item33 = doc.selectSingleNode("/dataList/data/item33");
Node item41 = doc.selectSingleNode("/dataList/data/item41");
Node item42 = doc.selectSingleNode("/dataList/data/item42");
Node item43 = doc.selectSingleNode("/dataList/data/item43");
Node item51 = doc.selectSingleNode("/dataList/data/item51");
Node item52 = doc.selectSingleNode("/dataList/data/item52");
Node item53 = doc.selectSingleNode("/dataList/data/item53");
Node item61 = doc.selectSingleNode("/dataList/data/item61");
Node item62 = doc.selectSingleNode("/dataList/data/item62");
Node item63 = doc.selectSingleNode("/dataList/data/item63");
Node item71 = doc.selectSingleNode("/dataList/data/item71");
Node item72 = doc.selectSingleNode("/dataList/data/item72");
Node item73 = doc.selectSingleNode("/dataList/data/item73");
Node item81 = doc.selectSingleNode("/dataList/data/item81");
Node item82 = doc.selectSingleNode("/dataList/data/item82");
Node item83 = doc.selectSingleNode("/dataList/data/item83");
Node temperature = doc.selectSingleNode("/dataList/data/temperature");
Node humidity = doc.selectSingleNode("/dataList/data/humidity");
Node item91 = doc.selectSingleNode("/dataList/data/item91");
Node item92 = doc.selectSingleNode("/dataList/data/item92");
Node item93 = doc.selectSingleNode("/dataList/data/item93");
Node item94 = doc.selectSingleNode("/dataList/data/item94");
Node item95 = doc.selectSingleNode("/dataList/data/item95");
Node item96 = doc.selectSingleNode("/dataList/data/item96");
Node item97 = doc.selectSingleNode("/dataList/data/item97");
Node item98 = doc.selectSingleNode("/dataList/data/item98");
Node item99 = doc.selectSingleNode("/dataList/data/item99");
Node item910 = doc.selectSingleNode("/dataList/data/item910");
Node item911 = doc.selectSingleNode("/dataList/data/item911");
Node item912 = doc.selectSingleNode("/dataList/data/item912");
Node item913 = doc.selectSingleNode("/dataList/data/item913");
Node item914 = doc.selectSingleNode("/dataList/data/item914");
Node item915 = doc.selectSingleNode("/dataList/data/item915");
Node item916 = doc.selectSingleNode("/dataList/data/item916");
Node item101 = doc.selectSingleNode("/dataList/data/item101");
Node item102 = doc.selectSingleNode("/dataList/data/item102");
Node item111 = doc.selectSingleNode("/dataList/data/item111");
Node item112 = doc.selectSingleNode("/dataList/data/item112");
WatTemperatureandhumidity wth = (WatTemperatureandhumidity) service
.getWaterTemperatureAndHumidity();
temperature.setText(wth.getTemperature().toString());
humidity.setText(wth.getHumidity().toString());
item1.setText("" + Math.floor(Math.random() * 20));
item2.setText("" + Math.floor(Math.random() * 99));
item3.setText("" + Math.floor(Math.random() * 20));
item4.setText("" + Math.floor(Math.random() * 99));
item5.setText("" + Math.floor(Math.random() * 20));
item6.setText("" + Math.floor(Math.random() * 99));
item7.setText("" + Math.floor(Math.random() * 20));
item8.setText("" + Math.floor(Math.random() * 99));
item9.setText("" + Math.floor(Math.random() * 20));
item10.setText("" + Math.floor(Math.random() * 99));
item11.setText("".equals(item12.getText()) ? "" : "44");
item12.setText("" + Math.floor(Math.random() * 99));
item13.setText("" + Math.floor(Math.random() * 20));
item21.setText("" + Math.floor(Math.random() * 99));
item22.setText("" + Math.floor(Math.random() * 20));
item23.setText("" + Math.floor(Math.random() * 99));
item31.setText("" + Math.floor(Math.random() * 20));
item32.setText("" + Math.floor(Math.random() * 99));
item33.setText("" + Math.floor(Math.random() * 20));
item41.setText("" + Math.floor(Math.random() * 99));
item42.setText("" + Math.floor(Math.random() * 20));
item43.setText("" + Math.floor(Math.random() * 99));
item51.setText("" + Math.floor(Math.random() * 99));
item52.setText("" + Math.floor(Math.random() * 20));
item53.setText("" + Math.floor(Math.random() * 99));
item61.setText("" + Math.floor(Math.random() * 20));
item62.setText("" + Math.floor(Math.random() * 99));
item63.setText("" + Math.floor(Math.random() * 20));
item71.setText("" + Math.floor(Math.random() * 99));
item72.setText("" + Math.floor(Math.random() * 20));
item73.setText("" + Math.floor(Math.random() * 99));
item81.setText("" + Math.floor(Math.random() * 20));
item82.setText("" + Math.floor(Math.random() * 99));
item83.setText("" + Math.floor(Math.random() * 20));
item91.setText("" + Math.floor(Math.random() * 20));
item92.setText("" + Math.floor(Math.random() * 99));
item93.setText("" + Math.floor(Math.random() * 20));
item94.setText("" + Math.floor(Math.random() * 99));
item95.setText("" + Math.floor(Math.random() * 20));
item96.setText("" + Math.floor(Math.random() * 99));
item97.setText("" + Math.floor(Math.random() * 20));
item98.setText("" + Math.floor(Math.random() * 99));
item99.setText("" + Math.floor(Math.random() * 20));
item910.setText("" + Math.floor(Math.random() * 99));
item911.setText("" + Math.floor(Math.random() * 20));
item912.setText("" + Math.floor(Math.random() * 99));
item913.setText("" + Math.floor(Math.random() * 20));
item914.setText("" + Math.floor(Math.random() * 99));
item915.setText("" + Math.floor(Math.random() * 20));
item916.setText("" + Math.floor(Math.random() * 99));
item101.setText("" + Math.floor(Math.random() * 20));
item102.setText("" + Math.floor(Math.random() * 99));
item111.setText("" + Math.floor(Math.random() * 20));
item112.setText("" + Math.floor(Math.random() * 99));
FileOutputStream fos = new FileOutputStream(url);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos,
"utf-8"));
XMLWriter writer = new XMLWriter(bw);
writer.write(doc);
bw.close();

return null;
lanzhengwu 2007-12-10
  • 打赏
  • 举报
回复
恩,程序有问题,把代码帖出来
lanzhengwu 2007-12-10
  • 打赏
  • 举报
回复
恩,程序有问题,把代码帖出来
吐司vivi 2007-12-10
  • 打赏
  • 举报
回复
你是怎么写的
stefli 2007-12-10
  • 打赏
  • 举报
回复
stefli 2007-12-10
  • 打赏
  • 举报
回复
eclipse3.1+tomcat5.0.27

很奇怪,过一定时间,就会写错一次。
我的操作是先读取data.xml文件,然后修改其中的部分数据,修改完后就保存回原来的文件(data.xml)去
结果就可以出现上面所说的问题。
issgates 2007-12-10
  • 打赏
  • 举报
回复
你是什么开发环境呢?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧