eHow launches Android app: Get the best of eHow on the go.

How To

How to Run a Script File in Java to Output an XML File

Contributor
By eHow Contributing Writer
(0 Ratings)

Creating XML files is used by programmers who want to create dynamic pages from Java code. XML files can be used to import values into a database, or they can be used to display lists of data to a browser. Java files can be used to generate the XML to a folder on the web server. The file can then be retrieved by an administrator or imported through code.

Difficulty: Moderate
Instructions
  1. 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.*;

  2. 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"));

  3. 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";

  4. 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";

  5. 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 +"\"?>");

  6. 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>");

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

  8. Step 8

    Close the file. Once records are created, closing the file releases the memory usage and unlocks it for further processes:
    xmlout.Close();

References

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2010 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

eHow Computers
eHow_eHow Technology and Electronics