private static void addImgToXmlData(String data) {
        try {
            File fXmlFile = new File(path);

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            Document dataDoc = dBuilder.parse(new InputSource(
                    new ByteArrayInputStream(data.getBytes("utf-8"))));

            doc.getDocumentElement().normalize();
            dataDoc.getDocumentElement().normalize();

            NodeList nListFromFile = doc.getElementsByTagName("gallery");
            Element eListFromData = (Element) dataDoc.getElementsByTagName(
                    "gallery").item(0);

            String galleryNameData = eListFromData.getAttribute("name");
            NodeList nListDataDoc = dataDoc.getElementsByTagName("image");

            int len = nListFromFile.getLength();
            for (int i = 0; i < len; i++) {
                Node eNode = nListFromFile.item(i);
                Element eElement = (Element) eNode;
                String name = eElement.getAttribute("name");
                if (name.equals(galleryNameData)) {
                    NodeList subNode = eElement.getElementsByTagName("image");
                    int leng = subNode.getLength();
                    for (int j = 0; j < leng; j++) {
                        Node image = subNode.item(0);
                        Element imageElement = (Element) image;
                        eElement.removeChild(imageElement);
                    }
                    leng = nListDataDoc.getLength();
                    for (int p = 0; p < leng; p++) {
                        Node imageDataDoc = nListDataDoc.item(p);
                        Element imageDataDocElement = (Element) imageDataDoc;
                        Node eDocImportedNode = doc.importNode(
                                imageDataDocElement, true);
                        eElement.appendChild(eDocImportedNode);
                    }
                }
            }
            doc.getDocumentElement().normalize();
            TransformerFactory transformerFactory = TransformerFactory
                    .newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(path));
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            transformer.transform(source, result);

            // System.out.println("XML file updated successfully");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }