这是我的xml文件。
- <deviceparameters> - <parameter componenttype="TextBox"> <name>Operating Type</name> <oid>1.3.6.1.4.1.31163.5.1.1</oid> <writable>true</writable> <description>The operating type defines which waveform type is used. This configuration takes several seconds to execute</description> - <paramvalues type="Integer"> <value default="No">123</value> </paramvalues> </parameter> - <parameter componenttype="TextBox"> <name>Active Waveform Status</name> <oid>1.3.6.1.4.1.31163.5.1.2</oid> <writable>false</writable> <description>Show the status of the waveform configured by operatingType</description> - <paramvalues type="String"> <value default="yes">Active</value> </paramvalues> </parameter> </deviceparameters>
我想删除名称为“ Active wavwform Status”的节点。如何从xml中删除该特定节点。
Active wavwform Status
我写的代码如下。
rootElement = doc.getDocumentElement(); NodeList nList = doc.getElementsByTagName("parameter"); String nodeName = TF_name.getText(); System.out.println(""+nList.getLength()); for (int temp = 0; temp < nList.getLength();temp++) { Node nNode = nList.item(temp); Element eElement = (Element) nNode; String upname1 = getTagValue("name", eElement); if(upname1.equals(nodeName)) { System.out.println("Parent: "+nNode.getParentNode().getNodeName()); System.out.println("nodename: "+nNode.getNodeName()); System.out.println("rmoving ...."); rootElement.removeChild(nNode); System.out.println("removed..."); } }
我得到了答案。 实际上,我是从xml文件中删除该节点,但没有将其写入xml文件中。 我缺少以下部分
TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(selectedFile); transformer.transform(source, result);
经过长时间的代码研究,我发现了这个愚蠢的错误,因此我纠正了它。 无论如何,谢谢您的回复。