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

本文介绍了解决Node.getTextContent()方法引起的编译错误的方法。通过调整项目的Java Build Path设置,将JRE System Library置于优先位置,并排除冲突的xml-apis.jar依赖,成功解决了编译问题。

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

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

package com.oplus.engineermode.charge.base; import android.text.TextUtils; import com.oplus.engineermode.core.sdk.utils.EngineerEnvironment; import com.oplus.engineermode.core.sdk.utils.Log; import com.oplus.engineermode.core.sdk.utils.XMLTolls; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.io.File; import java.util.Objects; public class WirelessPowerInfo { private static final String TAG = "WirelessPowerInfo"; private static final String WIRELESS_POWER_INFO_FILE = "wireless_power_info_new.xml"; private String mChargingMode; private String mTx; private String mRx; private String mK; public WirelessPowerInfo(String chargingMode, String tx, String rx, String k) { this.mChargingMode = chargingMode; this.mTx = tx; this.mRx = rx; this.mK = k; } public static boolean saveDataToFile(WirelessPowerInfo info, boolean debugEnable) { if (info == null) { return false; } String chargeModeString = info.getChargingMode(); if (!EngineerEnvironment.isExternalStorageMounted()) { Log.e(TAG, "external storage not mounted"); return false; } String filePath = getFilePath(); if (debugEnable) { Log.d(TAG, "filePath:" + filePath); } Document original = XMLTolls.loadWithDom(filePath); if (original != null) { Log.d(TAG, "FILE exists, search whether the target info exists"); NodeList itemNodeList = original.getElementsByTagName("Item"); if ((itemNodeList != null) && (itemNodeList.getLength() > 0)) { for (int i = 0; i < itemNodeList.getLength(); i++) { Node node = itemNodeList.item(i); NamedNodeMap temp = node.getAttributes(); if (temp != null) { for (int j = 0; j < temp.getLength(); j++) { Node tempNode = temp.item(j); if (debugEnable) { Log.d(TAG, "For search NAME:" + tempNode.getNodeName() + ",CONTENT:" + tempNode.getTextContent() + ",TYPE:" + tempNode.getNodeType() + ", VALUE:" + tempNode.getNodeValue()); } if (!TextUtils.isEmpty(chargeModeString) && chargeModeString.equals(tempNode.getNodeValue())) { Log.d(TAG, "hasFound target, remove the node, ensure that all write operations are the same"); node.getParentNode().removeChild(node); break; } } } } } else { Log.i(TAG, "File has no content"); } } else { Log.d(TAG, "FILE not exists or empty or invalid data, create file with xml style"); original = XMLTolls.init(); if (original != null) { Element element = original.createElement("WirelessData"); original.appendChild(element); XMLTolls.saveXmlWithDom(original, filePath); } } //after up operating, the file should exists or changed, so reparse the file original = XMLTolls.loadWithDom(filePath); if (original != null) { Node parent = original.getElementsByTagName("WirelessData").item(0); if (parent == null) { Log.i(TAG, "get element WirelessData failed, invalid initialization file"); return false; } Element tarEle = original.createElement("Item"); tarEle.setAttribute("name", chargeModeString); Element powerinfo = original.createElement("Element"); powerinfo.setAttribute("name", "TX"); powerinfo.setTextContent(info.getTx()); tarEle.appendChild(powerinfo); powerinfo = original.createElement("Element"); powerinfo.setAttribute("name", "RX"); powerinfo.setTextContent(info.getRx()); tarEle.appendChild(powerinfo); powerinfo = original.createElement("Element"); powerinfo.setAttribute("name", "K"); powerinfo.setTextContent(info.getK()); tarEle.appendChild(powerinfo); parent.appendChild(tarEle); return XMLTolls.saveXmlWithDom(original, filePath); } Log.i(TAG, "invalid initialization file"); return false; } public static String getFilePath() { return EngineerEnvironment.getExternalFilesDir(EngineerEnvironment.FILE_TYPE_OTHERS).getAbsolutePath() + File.separator + WIRELESS_POWER_INFO_FILE; } @Override public boolean equals(Object o) { if (this == o) { return true; } if ((o == null) || (getClass() != o.getClass())) { return false; } WirelessPowerInfo that = (WirelessPowerInfo) o; return Objects.equals(mChargingMode, that.mChargingMode) && Objects.equals(mTx, that.mTx) && Objects.equals(mRx, that.mRx) && Objects.equals(mK, that.mK); } @Override public int hashCode() { return Objects.hash(mChargingMode, mTx, mRx, mK); } public String getChargingMode() { return mChargingMode; } public String getTx() { return mTx; } public String getRx() { return mRx; } public String getK() { return mK; } public void setChargingMode(String mChargingMode) { this.mChargingMode = mChargingMode; } public void setTx(String mTx) { this.mTx = mTx; } public void setRx(String mRx) { this.mRx = mRx; } public void setK(String mK) { this.mK = mK; } @Override public String toString() { return "WirelessPowerInfo{" + "chargingMode='" + mChargingMode + '\'' + "tx='" + mTx + '\'' + ", rx='" + mRx + '\'' + ", k='" + mK + '\'' + '}'; } }
最新发布
06-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值