Pages

How to parse xml file in Java or how to read xml file in java is one of common need of a Java Developer working with enterprise Java application which uses XML for data representation, messaging and data transfer. Java has good support to handle XML files and XML Documents and you can read XML File in Java, create orwrite to XML file in Java by using various XML parsers available. Reading XML file is little bit different than reading text or binary file in Java but it uses same concept of File class.

Universal acceptability of XML and Java has helped them to grow together and they have lot of things common in between just like Java is platform independence, XML provides data which is also platform independent. You can use XML to transfer data between a legacy application written in C or C++ and Java.

What is important to work with XML in Java is correct understanding of XML Parser, Basic knowledge of XML document etc. In this Java XML Tutorial we will see how to parse and XML File by using both DOM XML Parser. We will also see difference between DOM and SAX parser in XMLand other basics related to XML parsing in Java. I thought about this article after sharing my xpath notes in Java.

How to read XML File in Java

JAXP - Java API for XML Parsing

Java provides extensive support for reading XML file, writing XML file and accessing any element from

XML file. All XML parsing related classes and methods are inside JAXP. Though DOM related code comes from org.w3c.dom package. All XML parsers are in javax.xml.parsers package.we will see example of parsing xml files using JAXP API in next section.

Parse XML File in Java using DOM Parser

In this section we will see how to parse xml files or how to read xml file using DOM XML Parser.DOM is quick and easy way to parse xml files in Java and if you are doing it for testing its way to go. Only thing to concern is that XML files which need to be parsed must not be too large. You can also create xml file by using DOM parser and DocumentFactory Class in Java.

XML file for parsing in Java

Here is xml file Stocks.xml which contains some stocks and there price, quantity we will use this in our xml parsing example in Java.

<?xmlversion="1.0"encoding="UTF-8"?>

<stocks>

<stock>

<symbol>Citibank</symbol>

<price>100</price>

<quantity>1000</quantity>

</stock>

<stock>

<symbol>Axis bank</symbol>

<price>90</price>

<quantity>2000</quantity>

</stock>

</stocks>

Code Example of Parsing XML File in Java using DOM Parser

Here is a code example of parsing above xml file in Java using DOM parser:

That’s all on xml parsing in java for now. We have seen how to read and write xml file in Java and now familiar with both DOM and SAX Parser in java. We will see more on xml on coming days like reading xml elements via xpath and using xml beans etc. let me know if you have any doubt on xml parsing example or in general with xml and Java.

@Anonymous, Thanks glad to hear that your like this Java XML tutorial on reading XML files. Sure, I will definitely try to write article on web services. by the way I have shared some web services Interview question on REST, you may find useful: REST and SOAP web service Interview Questions Answers

I was looking for a Simple Java program to read XML file which should be capable to read real world practical xml file, validate xml files against provided XSD or DTD etc. I can still use your Java program for first part just reading xml files but I am not sure how to validate XML files against XSD or DTD. Can you please help.

You can also use XML beans to read from XML File. xml beans are much simpler and provide more control. you just need an XSD file (XML Schema file), compile that schema and XML Bean compiler will generate classes which can than be used to read any content, element from XML file, update elements in XML and remove stuff from XML documents.

@Erieze, You can also modify or write data into XML using DOM or SAX parser, better way is to use XML Binding to Java objects for modifying XML documents form Java programming language. Open source library XML Beans or JAXB can be used to bind XML document to Java objects.

it is working fine in notepad+ but when i try to use the above 2 program in eclipse it is giving me an error. i have placed .xml file under the src folder and .java file under the package which is under the src folder again