mgbean.Listing.java Source code

Java tutorial

Introduction

Here is the source code for mgbean.Listing.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 mgbean;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import model.CD;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

@ManagedBean
@SessionScoped
public class Listing {

    private List<CD> cds;

    public Listing() {
        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();

            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);

            }

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

        }

    }

    public List<CD> getCds() {
        return cds;
    }

    public void setCds(List<CD> cds) {
        this.cds = cds;
    }
}