What is XML? [Java Code]

The XML (eXtensible Markup Language) is an extensible markup language similar to HTML but without predefined tags. Users create their own tags suited to their needs. It is an efficient way to store data in a universal format that is easy to search, share, and transfer over a network. The most important advantage of XML is that, the XML format is standardized and widely used. Almost all popular programming languages have built-in modules for parsing and processing XML. XML-formatted data is commonly used in client-server communication across the Internet between different systems. The Java code was automatically generated for the What Is XML example.
What is XML? [Java Code] Format
<?xml version = "1.0" encoding = "UTF-8"?>
<Order>
  <Id>78912</Id>
  <Customer>Jason Sweet</Customer>
  <Quantity>1</Quantity>
  <Price>18.00</Price>
</Order>
Updated: Viewed: 5234 times
Java code for What Is XML example

Java code for What Is XML Example

This Java code snippet was generated automatically for the What Is XML example.
<< Back to the What Is XML example

XML overview

XML stands for eXtensible Markup Language. XML was created to be a language with a simple formal syntax that makes it easy to transfer and manipulate on the Internet. XML is referred to as extensible because it does not fix the markup in documents: you can create markup that meets the needs of a specific area using only the syntax rules of the XML language. An XML document can contain multiple objects, each referring to another object. An XML document may consist of comments, elements, declarations, and processing instructions. Each of these components must be structured using XML markup.

XML Example
<?xml version = "1.0" encoding = "UTF-8"?>
<Request>
  <Login>
     login
  </Login>
  <Password>
     password
  </Password>
</Request>

XML Structure

The entire structure of XML and XML-based languages is built around tags that are used to create elements. The element is part of a web page. In XML and HTML, an element can contain a data element, a piece of text, an image, or possibly nothing. A typical element includes a start tag with some attributes, enclosing text content, and an end tag. XML declarations are processing instructions that identify a document as XML. All XML documents must begin with an XML declaration. XML - declaration is not a tag. Below is the XML structure with a detailed description:

XML Declaration Example
<?xml version = "1.0" encoding = "UTF-8"?>

1. XML tags

In XML, each element must be enclosed in tags. The tag is custom text wrapped in angle brackets. The text inside the angle brackets is the name of the tag.

<tag>...</tag>

2. XML root element

Every XML document has a root element. The Root element may have a custom name. The body of the XML document is located inside the Root element. In this XML example, the root element is called "Order":

<Order>
  <Customer>Jason Sweet</Customer>
</Order>

3. XML element value

The element's value is stored between the opening and closing tags. It can be a number, a string, or nested tags. In the XML example, the "Id" tag contains the value of the order.

<Order>
  <Id>78912</Id>
</Order>

What is the difference between XML and HTML?

The main difference between XML and HTML is that XML does not have predefined tags. In contrast, HTML is a markup language with predefined tags (which describe the structure of a web page).

Main difference:

  • XML tags are not predefined, whereas HTML has predefined tags
  • XML is case sensitive, and HTML is case insensitive
  • XML is content-driven while HTML is format driven
  • XML mainly focuses on passing data, while HTML focuses on presenting data
  • XML provides namespace support, while HTML does not provide namespace support
  • XML is strict for the closing tag while HTML is not
  • XML tags are extensible, whereas HTML has limited tags
  • XML is an abbreviation for Extensible Markup Language, while HTML stands for Hypertext Markup Language

How to add comments to XML?

Comments start in XML with "<!--" and end with "-->" and a text note should be added between these characters. Comments are for humans only. XML data processors ignore comments in XML. You can place comments anywhere within the XML.

XML Comment Example
<User>
  <!--Comment: The comment-->
  <Id>121</Id>
  <Name>Leo</Name>
</User>

How to display XML files?

XML is commonly used to store data. In order to reduce disk space, data in XML is stored in a minified form where all unnecessary line breaks and spaces are removed. In order to display XML in a human-readable form, it must first be formatted. Browsers can display XML documents in a convenient tree format. The browser renders raw XML if there is no XML rendering preference. XML output can be styled by specifying the CSS to apply to the document using the "xml-stylesheet" processing instruction.

<?xml-stylesheet type="text/css" href="stylesheet.css"?>

XML can be rendered by using XSLT, which can be used to transform it into other languages like HTML.

<?xml-stylesheet type="text/xsl" href="transform.xsl"?>

What are the benefits of using XML?

Different computer systems often store data in incompatible formats. Sharing data between incompatible systems is time-consuming for developers. When exchanging data, it is necessary to convert the data from one format to another so that the recipients can recognize and process it, and often incompatible data is lost. In contrast, data in XML is stored in a text format, and most modern programming languages have built-in modules for working with XML. Therefore, data in XML format can be stored, transported, and exchanged in a software and hardware-independent manner. By storing and transmitting data in XML format, the data can be accessed by all kinds of "reading machines" such as humans, computers, voice machines, news feeds, etc.

  • XML is extensible
  • XML simplifies data transfer
  • XML simplifies data exchange
  • XML simplifies data access

What is the difference between XML and JSON?

In the following table, you will find the main differences between XML and JSON:

XML JSON
XML data has no types JSON object has type
All XML data must be a string JSON types: string, number, array, boolean
XML data must be parsed Data is easily accessible as JSON objects
Cross-browser XML parsing can be tricky Most browsers support JSON
XML offers the ability to display data because it is a markup language JSON is not renderable
XML supports comments JSON does not support comments
XML documents are relatively harder to read and interpret JSON files are easier to read compared to XML
XML supports namespaces. It's more secure than JSON JSON does not provide any namespace support (less secure than XML)

See also

Generate code snippets for Java and other programming languages

Convert your What Is XML request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the Java code generator.