Java XML appendChild
Hello!I use org.w3c.dom (and I cant use any other) and now I need to be able to update a node and insert a node in a existing document.
I get the Document from a file with nodes like below. And I would for eg. be able to update the number node with a value since it is empty. But I can't figure out how since the Node in the this package is a Interface.
<student>
<firstname>John</firstname>
<lastname>Doe</lastname>
<number></number>
</student>
I have tried something like this below where I want to update node values from som text fields.:
for(int i = 0; i < textLabels.length; i++)
{
for(int j = 0; j < nodeList.getLength(); j++)
{
Node tempNode = nodeList.item(j);
if(tempNode.getNodeName().equals( textLabels[i].getText() ))
{
if(tempNode.getFirstChild() != null)
{
tempNode.getFirstChild().setNodeValue( valueTextFields[i].getText() );
}
else
{
//tempNode.setNodeValue( valueTextFields[i].getText() );
}
System.out.println(textLabels[i].getText() + " " + valueTextFields[i].getText() + " " + tempNode.getFirstChild().getNodeValue());
}
}
}
Best regards
Fredrik
