Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class Main {
    /**
     * Converts the XML file specified into the specified POJO type
     * @param <T> the object type of the POJO
     * @param xmlfile the XML file to convert
     * @param classOfT the class of the POJO
     * @return the POJO object if conversion was successful
     * @throws JAXBException
     * @throws XMLStreamException
     * @throws FileNotFoundException 
     */
    public static <T> T convertToPojo(File xmlfile, Class<T> classOfT)
            throws JAXBException, XMLStreamException, FileNotFoundException {
        JAXBContext jaxbContext = JAXBContext.newInstance(classOfT);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        XMLInputFactory xif = XMLInputFactory.newFactory();
        // settings to prevent xxe // would be funny if this tool is itsef is vulnerable to xxe :D
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);

        XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader(xmlfile));
        T t = (T) jaxbUnmarshaller.unmarshal(xsr);//(xmlfile);

        return t;
    }
}