Java XML Tutorial

java xml tutorials

In Java JDK, two built-in XML parsers are available – DOM and SAX, both have their pros and cons. Here’s few examples to show how to create, modify and read a XML file with Java DOM, SAX, JDOM.

In addition, updated JAXB example to show you how to convert object to / from XML.

DOM XML Parser

The DOM is the easiest to use Java XML Parser. It parses an entire XML document and load it into memory, modeling it with Object for easy nodel traversal. DOM Parser is slow and consume a lot memory if it load a XML document which contains a lot of data.

SAX XML Parser

SAX parser is work differently with DOM parser, it does not load any XML document into memory and create some object representation of the XML document. Instead, the SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure.

SAX Parser is faster and uses less memory than DOM parser.

JDOM XML Parser

JDOM provides a way to represent that document for easy and efficient reading, manipulation, and writing. It’s an alternative to DOM and SAX.

JAXB Example

JAXB, using annotation to convert Java object to / from XML file.

  • JAXB 2.0 hello world example
    A detail example to show you use JAXB to do XML Marshalling (convert object to XML) and XML Unmarshalling (Convert XML to object).

XML & Properties

The java.util.Properties class has build-in functionality to convert properties file into XML file or vice versse.

References

  1. Wiki Java_API_for_XML_Processing
  2. DOM XML parser documentation
  3. JDOM XML parser
  4. SAX useful quick start example
  5. SAX XML parser documentation
  6. JAXB Official Website
  7. Properties documentation
  8. Processing XML with Java

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
40 Comment threads
20 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
44 Comment authors
Sandeehowtonowloaderraton kumar mongolArjun vasudevan Recent comment authors
newest oldest most voted
java PDF
Guest
java PDF

How can I locate the Java cache, and is it safe to delete what is in it?

bill
Guest
bill

hi,

so you know how to convert xml file to strings using jdom?

thanks

trackback
Using XML

[…] sorts of things like data transfer, saving data, sending data, etc. Java API for XML Code Samples Java XML Tutorials Reply With […]

Me
Guest
Me

thx… this is how example and getting started should be… straight forward…good thx

Mohammed Hossain Doula
Guest
Mohammed Hossain Doula

There is not JAXB tutorial… Hopefully we will get that very soon…

Tutorials
Guest
Tutorials

Hello, Neat post. There’s an issue together with your web site in web explorer, may check this? IE still is the marketplace leader and a good part of folks will leave out your great writing due to this problem.

Shane P
Guest
Shane P

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

I think these 4 imports are necessary when reading from XML, I got errors without these imports? Any other solutions?

saikumar
Guest
saikumar

hi frnd,

i have a problem with xml i.e i want to get the data from xml in the form of java objects and send those java objects into the database….can you please help me …thanks

Ravi
Guest
Ravi

Hi, There is not FTP example. It will help me a lot if you put something.

Best Regards
Ravi

SATISH
Guest
SATISH

Hi Mkyong, I need to convert any xml file (having complex types) in to multiple csv files with proper mappings. I could see lots of utilities available in .net and c#.I am using one .net utility to convert an xml in to multiple csv files.But obviously there is a restriction that we can’t use this .net library on linux platform. Can u please provide me any java utility (which is purely built in java) , which does the same operation. Or do you have any algorithm with you so that i can implement in java? Your help is highly appreciated… Read more »

abdur
Guest
abdur

i want to modify the second record stored in the xml file but when i try to do this it also goes with the first record.obviously it have some solution please help me to find out the solution. my code is given bellow. import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.net.URL; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; //import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class ModifyXMLFile { public static void main(String[] args) { try { SAXBuilder builder = new SAXBuilder(); URL FileUrl = null; FileUrl = WriteXMLFile.class.getResource("../crypto"); String s=FileUrl.getPath(); System.out.println(s.substring(1)); File xmlFile = new File(s+"/file.xml"); Document doc = (Document) builder.build(xmlFile);… Read more »

mkyong reader
Guest
mkyong reader

How can we convert any xml to Java object? looking for generalized solution?

Darshan
Guest
Darshan

I am facing problem in parsing the XML using DOM on Android application.

I have data like “A & B” in XML tag. While parsing the XML, it only returns the “A” and ignoring everything after &. How to deal with this problem?

Yajairo87
Guest
Yajairo87

When do we should DOM XML Parser and when do we should use SAX XML Parser? What do we need to evaluate in order to choose one?

Thanks in advice

Ramulu
Guest
Ramulu

how to display all the missing tags and exceptions while validating a xml against xsd

trackback
Read a XML file with Java

[…] You should choose the correct XML parsers to read or parse XML file, like DOM or SAX, refer to this Java XML tutorials, a good start. Reply With Quote « Previous Thread | Next […]

Rupesh Patil
Guest
Rupesh Patil

Hello sir

I need help

sir i want java code that read xml file where tag name of xml file is unknown.

thanks

bradyliu
Guest
bradyliu

I like it

art
Guest
art

Hi mkyong,

I am your fan. I am learning java from you. Your tutorials are very easy explained .
Do you know how can I read an XML file and store its data into a database and from the database write an XML file
I’m driving crazy because I’m lost.

Cheers

Andy
Guest
Andy

Hey mykong,

thx alot for your examples, they are written verry nice and, as result, verry easy to learn from and to get some own ideas of.

Greetings, Andy

rafat
Guest
rafat

Hi brother
please help me i want to compare between xml files to extract the common tags and variably between this files please help me

ganeshkumar.t
Guest
ganeshkumar.t

HI MKY,
I want to transfer a very huge XML data from offline to online in a frequent manner.
Since the since is very huge , the data transfer is taking time.
Is thr any brain thoughts from your end to adopt to safer and tricky transfer ?
Advice pls on any algorithm or !!!! to shrink the data from offline and deflat in Online.

REgards
Ganesh K Thiagarajan

Collin
Guest
Collin

Very Helpful Tutorials!

amit
Guest
amit

How do we implement code for validation in java ???

Zhang Haihai
Guest
Zhang Haihai

Hi,

I want to select some info in xml, the info position in xml may be changeable. For example, it can be a value of an attribute, but we dont know the attribute name(It will be setted in the configuration file). So, can you suggest me a best choice above to do that.

Thanks a lot.

Saurabh Kapoor
Guest
Saurabh Kapoor

Hi!

I am coding to fetch up RSS feeds and display their contents as per user request. For fetching up RSS feeds, I am using SAX Parser to get only required tag values. But problem starts appearing with values containing special characters like ‘&’ etc. SAX Parser fetches up value only up to the point where & is present.
Can you provide a solution so that complete set of tag values can be retrieved using SAX Parser? Reason to implement this and not DOM is that XML file coming is quite large one.

Nirmal
Guest
Nirmal
XYZ
Guest
XYZ

ur site helps a lot

Gonzalo Abollado
Guest
Gonzalo Abollado

Hello,
Thanks for all the tutorials.
Very helpful.
I use your examples a lot.

Gonzalo.

vara prasad
Guest
vara prasad

I need to know about how to create a xml file which contains parameters of a table and loads it into a database table.?
please give me some suggestions and will you send the reply to my mail.?

in advance Thanks to you.