zoukankan      html  css  js  c++  java
  • 《转载》使用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>

    <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(简书作者)
    原文链接:http://www.jianshu.com/p/1205c9c5c7dc
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 相关阅读:
    1136.NumberSteps
    1134.密码翻译
    1133.学分绩点
    1131.合唱队形
    1132.与7无关的数
    1130.日志排序
    Educational Codeforces Round 41 (Rated for Div. 2)
    Codeforces Round #378 (Div. 2) F
    Codeforces Round #290 (Div. 2)
    牛客网练习13 乌龟跑步
  • 原文地址:https://www.cnblogs.com/abc8023/p/6179367.html
Copyright © 2011-2022 走看看