xmljdom.XMLJDOM.java Source code

Java tutorial

Introduction

Here is the source code for xmljdom.XMLJDOM.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package xmljdom;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.CD;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 *
 * @author joci
 */
public class XMLJDOM {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        SAXBuilder builder = new SAXBuilder();
        File f = new File("/home/joci/Joci/Java_Tanfolyam/cd_catalog.xml");

        try {
            Document document = builder.build(f);
            Element root = document.getRootElement();

            List<Element> elements = root.getChildren();
            List<CD> cds = new ArrayList<>();

            for (Element oneCD : elements) {

                CD cd = new CD(oneCD.getChildText("ARTIST"), oneCD.getChildText("TITLE"),
                        oneCD.getChildText("COMPANY"), oneCD.getChildText("COUNTRY"),
                        Double.parseDouble(oneCD.getChildText("PRICE")),
                        Integer.parseInt(oneCD.getChildText("YEAR")));
                cds.add(cd);

            }

            for (CD cd : cds) {
                System.out.println(cd);
            }

        } catch (JDOMException | IOException ex) {
            System.out.println(ex);
        }

    }

}