hi, I have a class test as shown below i have made a document object and added elements to it. I want ot know wether can i change the value of a Node directly without using the Functions Replacechild or Removechild etc If i get the node name from the getNodename() then i want to change the value of the node using setNodeValue() but getNodename returns a string. Is there any other method. Public class test { public static void main(String[] args) { try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element Record = document.createElement("Details"); Element LoginName = document.createElement("LoginName"); Text loginData = document.createTextNode("Felix"); LoginName.appendChild(loginData); Element PassWord = document.createElement("Password"); Text passData = document.createTextNode("Thomas"); PassWord.appendChild(passData); Element errorMessage = document.createElement("ErrorMessage"); Text errorData = document.createTextNode("What am i doing"); errorMessage.appendChild(errorData); Record.appendChild(LoginName); Record.appendChild(PassWord); Record.appendChild(errorMessage); document.appendChild(Record); System.out.println(errorData.getNodeValue()); // errorData.setNodeValue("Changed error Message"); // System.out.println(errorData.getNodeValue()); NodeList nodeList = document.getElementsByTagName("ErrorMessage"); Node nTmp ; if (nodeList != null) { for (int loopIndex = 0; loopIndex < nodeList.getLength();loopIndex++ ) { System.out.println(nodeList.item(loopIndex)); nTmp = nodeList.item(loopIndex); //nTmp.setNodeValue("Changed error Message"); System.out.println(nTmp.getNodeName()); // nTmp = (Node) nTmp.getNodeName() ; // String tt = nTmp.getNodeName(); System.out.println(nTmp.getNodeValue()); //String tt; } } // System.out.println(errorData.getNodeValue()); }catch(Exception e){ System.out.println(e); }
}
}
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
5
posted
0
If i get the node name from the getNodename() then i want to change the value of the node using setNodeValue() but getNodename returns a string.
Could you elaborate on this sentence a bit? You should be able to use org.w3c.dom.Node#setNodeValue(String) or org.w3c.dom.CharacterData#setData(String) to change the value.