• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

XML DOM query

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}

}

}
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Something about .... going for a swim. With this tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic