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

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

x
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.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
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
Java

From novice to tech pro — start learning today.