Firstly create a DOM (document object model) object representing your XML.
byte[] xmlBytes = msg.getBytes("UTF-8"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new ByteArrayInputStream(xmlBytes));
Then you need to add your new node to it:
Element newNode = doc.createElement("myNode"); newNode.setTextContent("contents of node"); Element root = doc.getDocumentElement(); // the <validateEmail> root.appendChild(newNode);
Then you want to write it to the filesystem, if I understand the question correctly.
File outputFile = ...; Source source = new DOMSource(doc); Result result = new StreamResult(outputFile); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result);