Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to a XML file in Java

Tags:

java

xml

I am working on the following code:

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;

public class CreatXMLFile {
  public static void main(String[] args) throws Exception {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
//    System.out.print("Enter number to add elements in your XML file: ");
//    String str = bf.readLine();
    int no=2;
//    System.out.print("Enter root: ");
    String root = "SMS";
    DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();

        DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();

        Document document = documentBuilder.newDocument();

    Element rootElement = document.createElement(root);

        document.appendChild(rootElement);
//    for (int i = 1; i <= no; i++)
//      System.out.print("Enter the element: ");
//      String element = bf.readLine();
      String element ="Number";
      System.out.print("Enter the Number: ");
      String data = bf.readLine();
      Element em = document.createElement(element);
      em.appendChild(document.createTextNode(data));
      rootElement.appendChild(em);

      String element1 ="message";
      System.out.print("Enter the SMS: ");
      String data1 = bf.readLine();
      Element em1 = document.createElement(element1);
      em1.appendChild(document.createTextNode(data1));
      rootElement.appendChild(em1);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result =  new StreamResult(System.out);
        transformer.transform(source, result);

  }
}

And it gives the following output:

run:
Enter the Number: 768678
Enter the SMS: ytu
<?xml version="1.0" encoding="UTF-8" standalone="no"?><SMS><Number>768678</Number><message>ytu</message></SMS>BUILD SUCCESSFUL (total time: 8 seconds)

Now I want to write the generated output (<?xml version="1.0" encoding="UTF-8" standalone="no"?><SMS><Number>768678</Number><message>ytu</message></SMS>) to a XML file on the hard disk. How do I do it?

like image 628
Ishan Avatar asked Mar 16 '10 09:03

Ishan


People also ask

Can we write Java code in XML file?

Read and Write XML with JAXB. JAXB stands for Java Architecture for XML Binding which provides a convenient way for manipulating XML in Java. It is Java standard that defines an API for reading and writing Java objects to and from XML documents. Starting from Java 6, JAXB is a part of the Java Development Kit (JDK).


Video Answer


2 Answers

Use a FileOutputStream (or a File) instead of System.out to construct your StreamResult.

like image 131
Joachim Sauer Avatar answered Sep 30 '22 05:09

Joachim Sauer


Here is the complete solution for this question :

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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

public class CreatXml {

    /**
     * @param args
     * @throws TransformerException 
     * @throws ParserConfigurationException 
     * @throws IOException 
     */
    public static void main(String[] args) throws TransformerException, ParserConfigurationException, IOException {
        // TODO Auto-generated method stub
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
//      System.out.print("Enter number to add elements in your XML file: ");
//      String str = bf.readLine();
        int no=2;
//      System.out.print("Enter root: ");
        String root = "SMS";
        DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();

            DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();

            Document document = documentBuilder.newDocument();

        Element rootElement = document.createElement(root);

            document.appendChild(rootElement);
//      for (int i = 1; i <= no; i++)
//        System.out.print("Enter the element: ");
//        String element = bf.readLine();
          String element ="Number";
          System.out.print("Enter the Number: ");
          String data = bf.readLine();
          Element em = document.createElement(element);
          em.appendChild(document.createTextNode(data));
          rootElement.appendChild(em);

          String element1 ="message";
          System.out.print("Enter the SMS: ");
          String data1 = bf.readLine();
          Element em1 = document.createElement(element1);
          em1.appendChild(document.createTextNode(data1));
          rootElement.appendChild(em1);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);

            StreamResult result =  new StreamResult(new StringWriter());

          //t.setParameter(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
            transformer.transform(source, result);

          //writing to file
          FileOutputStream fop = null;
          File file;
          try {

                file = new File("/home/zurelsoft/Desktop/newfile1.txt");
                fop = new FileOutputStream(file);

                // if file doesnt exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }

                // get the content in bytes
                String xmlString = result.getWriter().toString();
                System.out.println(xmlString);
                byte[] contentInBytes = xmlString.getBytes();

                fop.write(contentInBytes);
                fop.flush();
                fop.close();

                System.out.println("Done");

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fop != null) {
                        fop.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

    }

}
like image 32
Narayan Subedi Avatar answered Sep 30 '22 05:09

Narayan Subedi