Open append and save to a xml using java

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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.OutputKeys;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;

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);

        catch (Exception e) {

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.