EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial xml parsing with java
Secondary Sidebar
XML Tutorial
  • Basic
    • What is XML?
    • XML Tags
    • XML URL
    • XPath Sibling
    • XML root element
    • XML Encryption
    • XML Parsing php
    • xml parsing with java
    • Dataset XML
    • XML Parser in C#
    • XML Tree
    • XML boolean
    • XML sitemap
    • XML Array
    • XML reserved characters
    • XML Viewer
    • XML Uses
    • XML Special Characters
    • XML generator
    • XML file format
    • XML DOM
    • XML ampersand
    • XML Mapping
    • XML File
    • XML Element
    • XML HttpRequest
    • XML XSD
    • XML Schema
    • XML Namespaces
    • XML Comments
    • XML Attributes
    • XML Encoding
    • XML Validation
    • XML CDATA
    • XML Database
    • XML Technologies
    • XML Error
    • XML DTD
    • XML Date
    • XML Parsers
    • String in XML
    • XML with CSS
    • XML Versions
    • XML Features
    • XML Commands
    • Servlet web.xml
    • XPath Injection
    • XPath Functions
    • XPath starts-with
    • XPath Selector
    • XPath Count
    • XPath XML
    • XML Parsing in Oracle
    • XML parsing in python
  • Xpath
    • What is XPath?
    • XPath namespace
    • XPath for JSON
    • XPath Last Element
    • Xpath Substring
    • XPath First Element
    • XPath local-name
    • XPath Multiple Attributes
    • XPath Matches
    • XPath Position
    • XPath Index
    • XPath regex
    • XPath id contains
    • XPath innertext
    • XPath Multiple Conditions
    • XPath Helper
    • XPath Generator
    • XPath ID
    • XPath Locator

xml parsing with java

xml parsing with java

Introduction to xml parsing with java

XML stands for Extensive Markup Language created for encoding the documents by following the defined set of rules which makes the content in the readable format while the XML parser is used for modifying or accessing the data in XML files by going through the whole XML file. In java, the XML parser is a standalone component that is used for going through the XML documents.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,883 ratings)

What is xml parsing with java?

XML parsers are responsible for checking and validating the format of your XML document by scanning throughout the XML file and also provides the functionality to access or modify the data in it. The most important part in the process of development of XML is XML parsing. In java, the XML parser is a standalone component of XML that helps in parsing DTD standalone files, XML document or even XML schema. This parsed XML document can be further processed by the user. The below figure illustrates the process of parsing the XML documents in Java –

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What is xml parsing with java

https://docs.oracle.com/cd/B25016_08/doc/dl/web/B14033_01/adxdk002.gif

We can observe in the above figure that the input provided to parser includes the main XML document and other optional things that can be provided are schema files and DTD. Whatever output is generated from the parser is further passed to the DOM or SAX parser whichever you might be using as an input. The DOM parser or SAX parser also receives the XSL stylesheet file used for designing and beautifying the data. Further, the commands that are of parsed XSL along with the XML that is parsed are forwarded to XSLT processor that ultimately produces the transformed XML document as its output.

Steps to Using xml parsing with java

When parsing the XML documents, we need to follow the following steps listed below –

  • All the packages that are related to XML should be imported right in the beginning.
  • New instance of DocumentBuilder should be created.
  • From the available stream or file, you should create a document
  • The root element should be extracted.
  • Attributes and sub-elements should be examined.

Java XML Parser – DOM

Document Object Model (DOM) is a tree-based application programming interface creates a tree structure or representation inside the memory for the corresponding XML document passed to it. DOM comes along with the methods and classes that can be used for processing the tree and navigating through it inside the application.

Interface of DOM is the most useful component of XML tree which can be used for manipulations of structures. These manipulations involve deleting or adding old or new attributes and elements, element reordering, renaming the existing elements and many other.

DOM API is preferred for using when we need the random access of elements inside our application. DOM can also be used inside the XSL transformation tasks or while giving a call to XPath. In short, we can say that whenever there is a requirement to use the iterations in tree and need to scan or visit through the whole document then we can go for using the DOM API. It is also possible to customize the tree building process in DOM. In order to reduce the size of the pipes in XML documents, we can make more use of attributes instead of elements in DOM API

Create a DocumentBuilder- xml parsing with java

For creating an instance of document builder, we need to have the object of document builder factory class which can be created by using the code snippet shown below –

DocumentBuilderFactory instanceOfFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder instanceOFBuilder = instanceOfFactory.newDocumentBuilder();

Demo Example- xml parsing with java

Let us now consider one example that will help us to understand the implementation of all the steps mentioned above in “Steps to Using xml parsing with java” section that will make the understanding of Dom parser for XML document parsing clearer. We will have two files out of which one will be the main XML document which will be the input file for parsing and the other one will be java file which will parse the file and generate the output by accessing all the nodes of tree formed from XML file.

The file which needs to parse will be the XML file shown below –

xml file –

<?xml version = "1.0"?>
<class>
<article articleNo = "393">
<topic>Android Auto</topic>
<nameOfAuthor>Payal</nameOfAuthor>
<genre>Android Auto</genre>
<numberOfPages>85</numberOfPages>
</article>
<article articleNo = "493">
<topic>PostgreSQL</topic>
<nameOfAuthor>Mayur</nameOfAuthor>
<genre>Database</genre>
<numberOfPages>95</numberOfPages>
</article>
<article articleNo = "593">
<topic>MySQL</topic>
<nameOfAuthor>Meera</nameOfAuthor>
<genre>DBMS</genre>
<numberOfPages>90</numberOfPages>
</article>
</class>

Now, we need to write a class in java which can write the business logic as well as try to parse the XML file using DOM parser whose name is EducbaDomParserExample and the contents of the file are as shown below –

EducbaDomParserExample.java

package com.educba.xml;
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class EducbaDomParserExample {
public static void main(String[] args) {
try {
File xmlFileToParse = new File("educbaXML.txt");
DocumentBuilderFactory instanceOfDocBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder educbaDocBuilderObj = instanceOfDocBuilderFactory.newDocumentBuilder();
Document sampleDocument = educbaDocBuilderObj.parse(xmlFileToParse);
sampleDocument.getDocumentElement().normalize();
System.out.println("Element present at the root of the tree :" + sampleDocument.getDocumentElement().getNodeName());
NodeList nodeListInstance = sampleDocument.getElementsByTagName("article");
System.out.println("__________________________");
for (int temporaryVar = 0; temporaryVar < nodeListInstance.getLength(); temporaryVar++) {
Node singleNode = nodeListInstance.item(temporaryVar);
System.out.println("\nElement Being Traversed :" + singleNode.getNodeName());
if (singleNode.getNodeType() == Node.ELEMENT_NODE) {
Element particulerElement = (Element) singleNode;
System.out.println("Article Number : "
+ particulerElement.getAttribute("articleNo"));
System.out.println("Topic: "
+ particulerElement
.getElementsByTagName("topic")
.item(0)
.getTextContent());
System.out.println("Author Name : "
+ particulerElement
.getElementsByTagName("nameOfAuthor")
.item(0)
.getTextContent());
System.out.println("Genre: "
+ particulerElement
.getElementsByTagName("genre")
.item(0)
.getTextContent());
System.out.println("Number Of Pages : "
+ particulerElement
.getElementsByTagName("numberOfPages")
.item(0)
.getTextContent());
}
}
} catch (Exception sampleException) {
sampleException.printStackTrace();
}
}
}

The output of the execution of the above java file will produce the result shown in the below image –xml parsing with java output

Conclusion

The XML file or document which is an extensible markup language file will be parsed in java application by using various types of parsers such as DOM parser, SAX parser, etc. In this article, we saw, how we can convert that XML file into a tree-like structure showing all the hierarchies inside the memory and how we can access or modify each and individual element or attribute in java program.

Recommended Articles

This is a guide to xml parsing with java. Here we discuss how we can convert that XML file into a tree-like structure showing all the hierarchies inside the memory. You may also have a look at the following articles to learn more –

  1. XML Viewer
  2. XML Special Characters
  3. Rust XML
  4. Oracle XML
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more