我试图写一个XML文件,在我的XML文件中:
<user>
<name></name>
</user>
我可以写入XML文件的方法:
public void WriteToXML() throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputStream);
Element element = doc.getDocumentElement();
element.normalize();
NodeList nList = doc.getElementsByTagName("user");
Node node = nList.item(0);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element2 = (Element) node;
if(element2.getTagName() == "name")
{
element2.setNodeValue("SFDSFSDF");
}
}
}
然而,该方法被调用但由于某种原因它实际上并没有写入XML文件,因为当我读它时,它们实际上并不是XML中的任何内容吗?
答案 0 :(得分:0)
尝试替换:
element2.getTagName() == "name"
by:
(element2.getTagName()).equals("name")
另外,请尝试替换:
element2.setNodeValue("SFDSFSDF");
by:
element2.setTextContent("SFDSFSDF"); //adds content