Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to Write an XML file through a simple Java program ?.

Program to demonstrate how to Write an XML file in Java.

1. Create a simple POJO class whose properties needs to be stored in XML file.

package com.hubberspot.xml.writer;

public class Customer {

 private int customerId;
 private String customerName;
 private String complain;

 public Customer(int customerId, String customerName, String complain) {
  super();
  this.customerId = customerId;
  this.customerName = customerName;
  this.complain = complain;
 }


 public int getCustomerId() {
  return customerId;
 }


 public void setCustomerId(int customerId) {
  this.customerId = customerId;
 }


 public String getCustomerName() {
  return customerName;
 }


 public void setCustomerName(String customerName) {
  this.customerName = customerName;
 }


 public String getComplain() {
  return complain;
 }


 public void setComplain(String complain) {
  this.complain = complain;
 }

}




2. Create a Java class which will write into XML files through Java API.

package com.hubberspot.xml.writer;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream.GetField;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;


public class XmlWriterTest {

 public static void main(String[] args) {

  createXmlDocument("C://Customer.xml");

 } 

 private static Customer[] createCustomers() {

  Customer [] customers = new Customer[3];

  Customer customer1 = new Customer(1, "John Smith", "Internet Connection problem");
  Customer customer2 = new Customer(2, "Will Foster", "DTH Service problem");
  Customer customer3 = new Customer(3, "Jonty Rhodes", "Set-Top box not working");

  customers[0] = customer1;
  customers[1] = customer2;
  customers[2] = customer3;

  return customers;
 }

 private static void createXmlDocument(String fileInfo) {

  XMLOutputFactory factory = XMLOutputFactory.newFactory();

  FileOutputStream fos;
  XMLStreamWriter writer = null;

  try {

   fos = new FileOutputStream(fileInfo);
   writer = factory.createXMLStreamWriter(fos, "UTF-8");
  } 
  catch (FileNotFoundException e) {

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

   e.printStackTrace();
  }

  writeToDocument(writer);

 }

 private static void writeToDocument(XMLStreamWriter writer) {

  try {

   writer.writeStartDocument();
   writer.writeCharacters("\n");
   writer.writeStartElement("customers");
   writer.writeCharacters("\n");
   
   for(Customer customer : createCustomers()) { 
    
    writer.writeCharacters("\t");
    writer.writeStartElement("customer");
    writer.writeAttribute("id", String.valueOf(customer.getCustomerId()));
    
    writer.writeCharacters("\n\t\t");
    writer.writeStartElement("name");
    writer.writeCharacters(customer.getCustomerName());
    writer.writeEndElement();
    
    writer.writeCharacters("\n\t\t");
    writer.writeStartElement("complain");
    writer.writeCharacters(customer.getComplain());
    writer.writeEndElement();
    
    writer.writeCharacters("\n\t");
    writer.writeEndElement();
    writer.writeCharacters("\n");
   }
   
   writer.writeEndElement();
   writer.writeEndDocument();
   writer.close();

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



Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot