XML Processing in Java

XML is the short form of Extensible  Markup Language.XML is a famous data transport format. It is widely using in World Wide Web  for data transport.For example , if we are using a web service from a server to get a  piece of data from another server.In this case XML format  is   a good option  for the piece of data to be transferred .XML is defined in the XML 1.0 specification from w3c.Now XML is widely using in  data transport , configuration of various frameworks, Graphical  User Interface Development (For example layout in Android Development Kit )etc. This  chapter gives a basic idea about XML files.Also we are discussing about XML Processing in Java .

Basic Structure of XML file

 1)Every XML file starts with a prolog.The format of a prolog is something like  .

2)In XML  every piece of data is enclosed within start and end tags.There will be  one root element for an XML file. All other tags will be enclosed within the start and end tags of root element.Let us see an example XML file .


In the above example , the first line gives the prolog of the file.Here school is the root element.student element is enclosed within the school element.Each student element is having id , name and roll number as child elements.In this way data can be represented simply in XML.

Remember an  XML file is having extension .xml .

Advantages of XML over other file formats

1)It can be parsed easily

2)It is plain text

3)Transforming to other formats using XSLT   is possible.

XML Processing in Java

Java supports XML parsing . There are various APIs and tools available for XML processing. Some of them are listed below.We will see each in detail in the coming sections.

1)DOM Interface

2)SAX  Interface

3)StAX Interface


Of the above four , first 3 are part of JAXP(Java API  for XML Processing). These interfaces are available  in Java   from Java 1.5 onwards.

JAXB is comparatively new  technique. JAXB is the abbreviation of Java API for XML Binding.It allows mapping between XML and Java class.JAXB is inbuilt with SE 1.6 onwards.

There can be other options too . I am only explaining the parsers I have worked till now. Sample codes for each of the above will be explained in the coming sections