import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class Main {
public static void main(String[] args) throws Exception {
DocumentBuilder db = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(
"<emp><empname><firstName></firstName><lastName></lastName></empname></emp>")));
NodeList customerNodes = doc.getElementsByTagName("empname");
for (int i = 0; i < customerNodes.getLength(); i++) {
NodeList children = customerNodes.item(i).getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
String childNode = children.item(j).getNodeName();
if (childNode.equalsIgnoreCase("firstName")) {
children.item(j).setTextContent(String.valueOf("John"));
} else if (childNode.equalsIgnoreCase("lastName")) {
children.item(j).setTextContent(String.valueOf("Doe"));
}
}
}