Saturday, May 18, 2024
HomeJavaJDOM Instance : Studying and Parsing XML with SAX parser in Java

JDOM Instance : Studying and Parsing XML with SAX parser in Java


Disclosure: This text might include affiliate hyperlinks. While you buy, we might earn a fee.

XML parsing with JDOM parser

JDOM is an open supply library which permit XML parsing and studying in Java program. JDOM is designed by utilizing Java programming approach and customised for Java programmers, in order that Java programmer with  little or no information of XML paperwork can use JDOM to learn XML information. Not like DOM Parser of Java API , which makes use of Manufacturing unit design sample to create occasion of parser e.g DocumentBuilderFactory and DocumentBuilder, as seen in our final instance of parsing XML paperwork in Java, JDOM makes use of new() operator to create its parser situations. In actual fact JDOM may be very simple to grasp and more often than not its self explanatory. 
Whereas attempting this Java program to parse XML file, you may discover JDOM API. JDOM API additionally permits to make use of XPATH expression to question XML paperwork with package deal org.jdom2.xpath. In subsequent part we are going to see full code instance of Easy methods to learn an XML file utilizing JDOM parser in Java program.

Easy methods to learn XML file utilizing JDOM in Java

How to read XML file using JDOM parser in Java exampleOn this code instance we are going to use following XML file to display How JDOM learn this XML file and supply entry to root, numerous parts and attributes.

Right here is our pattern XML file:

<?xml model=“1.0” encoding=“UTF-8”?>
<books>
        <e book ISBN=“1234”>
                <creator>James</creator>
                <title>Few phrases</title>
                <pages>120</pages>
                <language>English</language>
        </e book>
        <e book ISBN=“5678”>
                <creator>Peter</creator>
                <title>Across the World</title>
                <pages>200</pages>
                <language>English</language>
        </e book>
</books>

and here’s a Java program which reads this XML doc utilizing JDOM library :

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Checklist;

import org.jdom2.Doc;
import org.jdom2.Factor;
import org.jdom2.JDOMException;
import org.jdom2.enter.SAXBuilder;

/**
 * Java program to learn XML file utilizing JDOM library.
 * JDOM is an open supply library which is near Java fashion
 * and permits programmer to learn and write XML information in JAva
 *
 * @creator
 */

public class XMLReader {

        public static void primary(String args[]){
             
                //creating JDOM SAX parser
                SAXBuilder builder = new SAXBuilder();
             
                //studying XML doc
                Doc xml = null;
                strive {
                        xml = builder.construct(new File(“check.xml”));
                } catch (JDOMException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
             
                //getting root factor from XML doc
                Factor root = xml.getRootElement();
             
                System.out.println(“Root factor of XML doc is : “ + root.getName());
                System.out.println(“Variety of books on this XML : “ + root.getChildren().dimension());
             
                Checklist<Factor> books = root.getChildren();
             
                //Iterating over all childs in XML
                Iterator<Factor> itr = books.iterator();
                whereas(itr.hasNext()){
                        Factor e book = itr.subsequent();
                        //studying attribute from Factor utilizing JDOM
                        System.out.println(“ISBN : “ + e book.getAttributeValue(“ISBN”));
                     
                        //studying worth of childern in XML utilizing JDOM
                        System.out.println(“creator : “ + e book.getChildText(“creator”));
                        System.out.println(“title : “ + e book.getChildText(“title”));
                        System.out.println(“pages : “ + e book.getChildText(“pages”));
                        System.out.println(“language : “ + e book.getChildText(“language”));
                }
        }
}

Output:
Root factor of XML doc is : books
Quantity of books in this XML : 2
ISBN : 1234
creator : James
title : Few phrases
pages : 120
language : English
ISBN : 5678
creator : Peter
title : Across the World
pages : 200
language : English

When you have a look at this instance of parsing XML doc utilizing JDOM parser, you will see it surprisingly easy. you simply must create SAXBuilder and you then bought Doc object, from you could get Factor and correspondingly worth of kid and attributes.

That is all on Easy methods to learn XML file utilizing JDOM open supply library. JDOM may be very simple to make use of library for Java programmer and nice for rapidly studying XML paperwork in Java.

Different Java Tutorials chances are you’ll like

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments