Monday, March 30, 2020

Jdom Illustration : Reading Too Parsing Xml Amongst Sax Parser Inward Java

XML parsing amongst JDOM parser
JDOM is an opened upward source library which allow XML parsing as well as reading inward Java program. JDOM is designed yesteryear using Java programming technique as well as customized for Java programmers, thence that Java programmer with  very footling noesis of XML documents tin role JDOM to read XML files. Unlike DOM Parser of Java API , which uses Factory blueprint pattern to exercise event of parser e.g DocumentBuilderFactory as well as DocumentBuilder, every bit seen inward our concluding illustration of parsing XML documents inward Java, JDOM uses new() operator to exercise its parser instances. In fact JDOM is real slow to sympathize as well as almost of the fourth dimension its self explanatory. While trying this Java programme to parse XML file, yous tin explore JDOM API. JDOM API likewise allows to role XPATH expression to interrogation XML documents amongst packet org.jdom2.xpath. In adjacent department nosotros volition encounter consummate code illustration of How to read an XML file using JDOM parser inward Java program.



How to read XML file using JDOM inward Java

read this XML file as well as render access to root, diverse elements as well as attributes.




Here is our sample XML file:
<?xml version="1.0" encoding="UTF-8"?>
<books>
        <book ISBN="1234">
                <author>James</author>
                <title>Few words</title>
                <pages>120</pages>
                <language>English</language>
        </book>
        <book ISBN="5678">
                <author>Peter</author>
                <title>Around the World</title>
                <pages>200</pages>
                <language>English</language>
        </book>
</books>

and hither is a Java programme which reads this XML document using JDOM library :

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

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 * Java programme to read XML file using JDOM library.
 * JDOM is an opened upward source library which is around Java style
 * as well as allows programmer to read as well as write XML files inward JAva
 *
 * @author
 */


public class XMLReader {

        public static void main(String args[]){
             
                //creating JDOM SAX parser
                SAXBuilder builder = new SAXBuilder();
             
                //reading XML document
                Document xml = null;
                try {
                        xml = builder.build(new File("test.xml"));
                } catch (JDOMException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
             
                //getting beginning chemical constituent from XML document
                Element beginning = xml.getRootElement();
             
                System.out.println("Root chemical constituent of XML document is : " + root.getName());
                System.out.println("Number of books inward this XML : " + root.getChildren().size());
             
                List<Element> books = root.getChildren();
             
                //Iterating over all childs inward XML
                Iterator<Element> itr = books.iterator();
                while(itr.hasNext()){
                        Element majority = itr.next();
                        //reading attribute from Element using JDOM
                        System.out.println("ISBN : " + book.getAttributeValue("ISBN"));
                     
                        //reading value of childern inward XML using JDOM
                        System.out.println("author : " + book.getChildText("author"));
                        System.out.println("title : " + book.getChildText("title"));
                        System.out.println("pages : " + book.getChildText("pages"));
                        System.out.println("language : " + book.getChildText("language"));
                }
        }
}

Output:
Root chemical constituent of XML document is : books
Number of books inward this XML : 2
ISBN : 1234
author : James
title : Few words
pages : 120
language : English
ISBN : 5678
author : Peter
title : Around the World
pages : 200
language : English

If yous await at this illustration of parsing XML document using JDOM parser, yous volition abide by it surprisingly simple. yous merely need to exercise SAXBuilder as well as and thence yous got Document object, from that yous tin larn Element as well as correspondingly value of kid as well as attributes.

That's all on How to read XML file using JDOM opened upward source library. JDOM is real slow to role library for Java programmer as well as non bad for speedily reading XML documents inward Java.

Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services as well as REST API amongst Spring Boot
Difference betwixt TreeSet as well as TreeMap inward Java

No comments:

Post a Comment