如何修改DOM树中的一个结点值
list.xml:
<?xml version="1.0" encoding="utf-8"?>
<markbook>
<notes>
<Fir>151</Fir>
<Sec>130</Sec>
<Thr>76</Thr>
<Fou>25</Fou>
</notes>
</markbook>
现有一个结点为:Fir,我想修改它的值,用了以下代码://fir我从外读取的String类数据
note.getElementsByTagName("Fir").item(0).getFirstChild().setNodeValue(fir);
其中:这个程序我是用JAVA写的,note是这样得到的:
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
doc=builder.parse("list.xml");
doc.normalize();
NodeList notes=doc.getElementsByTagName("notes");
note=(Element)notes.item(0);
对于这些结点我可以读取数据值,但是修改它们的值却不行,从不改不了,还请高手帮忙~~
[解决办法]
用setTextContent, 不是setNodeValue
[解决办法]
package com.aowin.sms.util;import java.io.File;import java.io.IOException;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;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;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Text;import org.xml.sax.SAXException;public class XmlFile { private DocumentBuilderFactory dbf = null; private DocumentBuilder db = null; private Document d = null; private File file = null; public XmlFile(String fileName) { this(new File(fileName)); } public XmlFile(File file) { try { dbf = DocumentBuilderFactory.newInstance(); db = dbf.newDocumentBuilder(); this.file = file; } catch (ParserConfigurationException e) { System.out.println(e.getMessage()); } } public void createRootElement(String name) { d = db.newDocument(); Element root = d.createElement(name); d.appendChild(root); write(); } public void appendElement(String tagname, String name) { appendElement(tagname, name, 0); } public void appendElement(String tagname, String name, int index){ try { d = db.parse(file); Element child = d.createElement(name); NodeList fe = d.getElementsByTagName(tagname); Node father = fe.item(index); father.appendChild(child); write(); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } public void appendElement(String tagname, String name, String value, int index){ try { d = db.parse(file); NodeList fe = d.getElementsByTagName(tagname); Node father = null; if (fe.getLength() > 0) { Element child = d.createElement(name); Text text = d.createTextNode(value); child.appendChild(text); father = fe.item(index); father.appendChild(child); write(); } } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } public void appendElement(String tagname, String name, String value) { appendElement(tagname, name, value, 0); } public void setValue(String tagname, String value, int index){ try { d = db.parse(file); NodeList fe = d.getElementsByTagName(tagname); if (fe.getLength() > 0) { Node father = fe.item(index); Text text = d.createTextNode(value); Node child = father.getFirstChild(); child.setNodeValue(value); } write(); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } public void setValue(String tagname, String value) { setValue(tagname, value, 0); } private void write() { try { TransformerFactory tff = TransformerFactory.newInstance(); Transformer tf = tff.newTransformer(); DOMSource ds = new DOMSource(d); StreamResult sr = new StreamResult(file); tf.transform(ds, sr); } catch (TransformerConfigurationException e) { System.out.println(e.getMessage()); } catch (TransformerException e) { System.out.println(e.getMessage()); } } public String getValue(String tagname) { return getValue(tagname, 0); } public String getValue(String tagname, int index) { String value = null; try { d = db.parse(file); NodeList father = d.getElementsByTagName(tagname); if (father.getLength() > 0) { Node child = father.item(0); value = child.getTextContent(); } } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } return value; } public int getLength(String tagname){ int length = 0; try { d = db.parse(file); NodeList father = d.getElementsByTagName(tagname); length = father.getLength(); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } return length; } public static void main(String[] args) { XmlFile xf = new XmlFile("application.xml"); // xf.setValue("driver", "asdf"); // xf.setValue("server", "lasdf06"); // xf.setValue("database", "gasdf"); // xf.setValue("user", "gad"); // xf.setValue("password", "1asdg"); System.out.println(xf.getValue("driver")); System.out.println(xf.getValue("server")); System.out.println(xf.getValue("database")); System.out.println(xf.getValue("user")); System.out.println(xf.getValue("password")); }}