This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes XML DOM query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML DOM query" Watch "XML DOM query" New topic

XML DOM query

felix thomas
Ranch Hand

Joined: Jun 10, 2001
Posts: 89
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)

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");
Element PassWord = document.createElement("Password");
Text passData = document.createTextNode("Thomas");
Element errorMessage = document.createElement("ErrorMessage");
Text errorData = document.createTextNode("What am i doing");
// 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++ )
nTmp = nodeList.item(loopIndex);
//nTmp.setNodeValue("Changed error Message");
// nTmp = (Node) nTmp.getNodeName() ;
// String tt = nTmp.getNodeName();
//String tt;
// System.out.println(errorData.getNodeValue());
}catch(Exception e){


Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
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.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: XML DOM query
It's not a secret anymore!