org.w3c.dom.Element 缺少 setTextContent 方法

本文介绍了解决在从JDK5升级到JDK6后,Eclipse中出现org.w3c.dom.Element没有setTextContent方法的问题。通过排除旧版本的xml-api.jar解决了编译错误。

今天将项目环境由jdk5改为jdk6,eclipse重新编译工程后,却突然出现org.w3c.dom.Element没有setTextContent方法 的编译错。

 

放狗搜后,发现主要是 xercesImpl.jar 和 xml-apis.jar的Element版本太老造成的,setTextContent是DOM3中的方法。

 

但在pom文件中,未发现有直接引用这两个jar包的地方。好在eclipse maven插件功能强大,在Project Explorer的Maven Dependencies中

 

找到了xml-api.jar,然后右键->Maven->Exclude Maven artifact,弹出窗口,提示会在当前pom文件中的hibernate-core依赖中,排除掉

 

xml-api.jar,确认后,pom文件被修改,更新Maven Dependencies,重新编译,问题解决

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
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值