org.w3c.dom.Element setTextContent方法确实问题

本文详细介绍了如何通过调整Java Build Path中的JRE System Library位置来解决Java编译错误,指出冲突的jar包可能是rt.jar、xml-apis.jar等,并提供了排除冲突的方法。最终通过修改pom.xml文件中的依赖排除实现了解决方案。

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


 

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置顶。然后再进行编译即可。如图: 


 

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


其实顺着问题的解决思路想想,肯定是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的依赖而引入的。如图: 


 

解决方法:右击该jar,选择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

import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; public class GenerateBookXML { public static void main(String[] args) { try { // 创建一个 DocumentBuilderFactory 实例 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); // 创建一个 DocumentBuilder 实例 DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // 创建根元素 <books> Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("books"); doc.appendChild(rootElement); // 创建第一本图书元素 Element book1 = doc.createElement("book"); rootElement.appendChild(book1); // 添加图书 1 的属性 book1.setAttribute("id", "1"); // 添加图书 1 的子元素 Element title1 = doc.createElement("title"); title1.setTextContent("Java Programming"); book1.appendChild(title1); Element author1 = doc.createElement("author"); author1.setTextContent("John Doe"); book1.appendChild(author1); // 创建第二本图书元素 Element book2 = doc.createElement("book"); rootElement.appendChild(book2); // 添加图书 2 的属性 book2.setAttribute("id", "2"); // 添加图书 2 的子元素 Element title2 = doc.createElement("title"); title2.setTextContent("XML in Action"); book2.appendChild(title2); Element author2 = doc.createElement("author"); author2.setTextContent("Jane Smith"); book2.appendChild(author2); // 将 DOM 树保存为 XML 文件 TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // 设置输出格式为美化打印 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult("ex-ch08.xml"); transformer.transform(source, result); System.out.println("XML 文件已生成:ex-ch08.xml"); } catch (ParserConfigurationException | TransformerException e) { e.printStackTrace(); } } } 这个的运行结果大概是什么样子的
最新发布
10-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值