What are the benefits of XML?

There are many benefits of using XML on the Web:

  • Simplicity– Information coded in XML is easy to read and understand, plus it can be processed easily by computers.
  • Openness– XML is a W3C standard, endorsed by software industry market leaders.
  • Extensibility – There is no fixed set of tags. New tags can be created as they are needed.
  • Self-description– In traditional databases, data records require schemas set up by the database administrator. XML documents can be stored without such definitions, because they contain meta data in the form of tags and attributes.
  • Contains machine-readable context information- Tags, attributes and element structure provide context information that can be used to interpret the meaning of content, opening up new possibilities for highly efficient search engines, intelligent data mining, agents, etc.
  • Separates content from presentation– XML tags describe meaning not presentation. The motto of HTML is: “I know how it looks”, whereas the motto of XML is: “I know what it means, and you tell me how it should look.” The look and feel of an XML document can be controlled by XSL style sheets, allowing the look of a document to be changed without touching the content of the document. Multiple views or presentations of the same content are easily rendered.
  • Supports multilingual documents and Unicode-This is important for the internationalization of applications.
  • Facilitates the comparison and aggregation of data – The tree structure of XML documents allows documents to be compared and aggregated efficiently element by element.
  • Can embed multiple data types – XML documents can contain any possible data type – from multimedia data (image, sound and video) to active components (Java applets, ActiveX).
  • Can embed existing data – Mapping existing data structures like file systems or relational databases to XML is simple. XML supports multiple data formats and can cover all existing data structures.
  • Provides a ‘one-server view’ for distributed data – XML documents can consist of nested elements that are distributed over multiple remote servers. XML is currently the most sophisticated format for distributed data – the World Wide Web can be seen as one huge XML database.

How to create XML file in Java

package com.test;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CreateXMLInJava {

public static void main(String argv[]) {
CreateXMLInJava createXml = new CreateXMLInJava();

public void createXML() {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement(“Company”);

// staff elements
Element staff = doc.createElement(“Dept”);

// set attribute to staff element
Attr attr = doc.createAttribute(“id”);

// can be written as
// staff.setAttribute(“id”, “1”);

// firstname elements
Element firstname = doc.createElement(“Firstname”);

// lastname elements
Element lastname = doc.createElement(“Lastname”);

// salary elements
Element salary = doc.createElement(“Salary”);

// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(“D:\\test.xml”));

// Output to console for testing
// StreamResult result = new StreamResult(System.out);

transformer.transform(source, result);

System.out.println(“File saved!”);

} catch (ParserConfigurationException pce) {
} catch (TransformerException tfe) {