• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

Open append and save to a xml using java

Hi,
I am using the following line to open and append to an xml

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
 Mapper mapper=new Mapper();
 Document doc = docBuilder.parse (new File("test/regression/qc/KeywordApproachInput.xml"));
.
.
.
//adding some attributes and their value
NodeList Steps=thisIteration.getElementsByTagName("step");
for (int s=0;s<Steps.getLength();s++){
thisStep.setAttribute("abc", "xyz");
thisStep.setAttribute("def", "ijk");
}

Now how do i save the xml file.

Please ignore the variables i have used in the code.
0
PearlJamFanatic
Asked:
PearlJamFanatic
  • 2
3 Solutions
 
Pramod KumarCommented:
Try below sample code. This will save the new XML and also display the new xml:

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import java.io.File;
import java.io.StringWriter;
import java.io.FileOutputStream;

public class WriteXML {
    public static void main(String[] args) {
        try {
            String filename = "KeywordApproachInput.xml";
            File file = new File(filename);
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();

            Document doc = builder.parse(file);
            String root = doc.getDocumentElement().getNodeName();

            NodeList stepNodeList = doc.getElementsByTagName("step");

            for (int s = 0; s < stepNodeList.getLength(); s++) {
                Element stepElement = (Element) stepNodeList.item(s);
                stepElement.setAttribute("abc", "xyz");
                stepElement.setAttribute("def", "ijk");                
            }

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            //initialize StreamResult with File object to save to file
            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(doc);
            transformer.transform(source, result);

            String xmlString = result.getWriter().toString();
            FileOutputStream out = new FileOutputStream(filename);
            out.write(xmlString.getBytes());
            out.close();
            System.out.println(xmlString);

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

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now