使用org.w3c.dom.Element的setTextContent()、getTextContent()方法时出现编译错误

今天在更新项目后进行编译时,出现如下错误一堆:
编译错误

Google之,在stackoverflow上看到如下的解决方法:

I came here with the same problem. Even worse: I had two projects side by side, both targetting the same JRE (1.6), and one was able to resolve Node.getTextContent() while the other wasn’t. I resolved it sort of by accident; I went to project properties | Java Build Path | Order and Export tab, selected the JRE (which was at the bottom of the list) and clicked the “Top” button to move it to the top. My problem went away. It appears that the Node I wanted was hidden by another one. :-\ Maybe this will help with your problem.

大体解决方法就是:
在项目的Java Build Path | Order and Export选项卡中,将JRE System Library选中,并Top置顶。然后再进行编译即可。如图:
top-jre6

但是上面并没有给出原因。


其实顺着问题的解决思路想想,肯定是jar出现了冲突所致。于是我就在项目的jar包中找可能含有org.w3c.dom.Element这个类的jar包。既然将JRE的lib进行了置顶,那么就有理由猜测JRE-lib里存在这个类的相关方法。

最终,在rt.jarxml-apis.jar和中找到了。应该就是这两个jar冲突所致,由于引用优先级的不同导致引用了xml-apis.jar中的方法。

其实在pom.xml中并没有这个jar的直接引用,在Dependency Hierarchy视图中搜索xml-apis可以发现,它其实是由于dom4j的依赖而引入的。如图:
Dependency  Hierarchy

解决方法:右击该jar,选择exclude maven artifact,确认并保存,重新编译即可:
exclude maven artifact

最终的pom.xml中只是在dom4j<dependency>中多了这么一段<exclusions>

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<exclusions>
<exclusion>
<artifactId>xml-apis</artifactId>
<groupId>xml-apis</groupId>
</exclusion>
</exclusions>
</dependency>

参考:
http://stackoverflow.com/questions/5534864/compilation-error-in-node-gettextcontent-for-jdk-6
http://www.educity.cn/wenda/364108.html

hoxis wechat
一个脱离了高级趣味的程序员,关注回复1024有惊喜~
赞赏一杯咖啡
0%