Node.getTextContent() not found 解决办法

本文介绍了在使用Node.getTextContent()方法时遇到的问题及解决办法。该问题是由于xml-api.jar包导致的jar包冲突,即使项目中未直接使用此包也会受到影响。通过检查依赖发现是xalan包引入了冲突的xml-api.jar,最终解决方案为移除xalan。
Node.getTextContent()是org.w3c.dom.Node下面的方法,这个包是JDK自带的,这所以会出现getTextContent找不到是因为jar包的冲突,是由xml-api.jar这个包的冲突引起的。我的工程里没有使用xml-api.jar,查了下包依赖,是xalan依赖了xml-api.jar,删除xalan就好了
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、付费专栏及课程。

余额充值