-
Step 1
Import the necessary libraries. The IO library contains the classes needed to generate files including XML. The following is the syntax used for importing libraries:
import java.io.*; -
Step 2
Create the file variable. This following code instantiates the class, while creating an XML file at the same time:
PrintWriter xmlout = new PrintWriter(new FileOutputStream("customers.xml")); -
Step 3
Create the encoding string. This string is used by web browsers to detect the type of characters used in the XML file. The following string is used to save the encoding characters:
String myEnc = "ISO-8859-1"; -
Step 4
Create some basic information to populate the XML file. In this example, a list of customers is created. The syntax below creates a customer that will be exported as XML.
String myCustomerName = "Joe";
String myCustomerId = "33"; -
Step 5
Write the first line, which is the encoding. Encoding directives are always the first line of a browser file.
xmlout.println("<?xml version=\"1.0\" encoding=\""+myEnc +"\"?>"); -
Step 6
Create the opening customer XML tag. The following syntax starts the list of customers with the opening "customers" tag. All customer records are located within this tag.
xmlout.println("<customers>"); -
Step 7
Write the customer record to the xml file. The following code writes a record within the top level "customers" tag:
xmlout.println("<customer customerId=\""+myCustomerId+"\" name=\""+myCustomerName+"\"></customer>"); -
Step 8
Close the file. Once records are created, closing the file releases the memory usage and unlocks it for further processes:
xmlout.Close();












