Java XML JAXB Context parseXMLFile(String fileName, String contextPath)

Here you can find the source of parseXMLFile(String fileName, String contextPath)

Description

Parses the given XML file.

License

Apache License

Parameter

Parameter Description
T the expected return type
fileName the name of the file to parse
contextPath the context path for JAXB

Exception

Parameter Description
FileNotFoundException if file not found
JAXBException if parsing failed

Return

the root of the parsed document as the type specified

Declaration

public static <T> T parseXMLFile(String fileName, String contextPath)
        throws FileNotFoundException, JAXBException 

Method Source Code

//package com.java2s;
/**//from  ww w .j  a v a 2s .  c o  m
 * Copyright 2014 SAP AG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.FileNotFoundException;
import java.io.FileReader;

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

import javax.xml.bind.Unmarshaller;

public class Main {
    /**
     * Parses the given XML file. Under the provided context path an object
     * factory for JAXB is expected. Thus the context path is most likely
     * <code>ObjectFactory.class.getPackage().getName()</code> whereas the
     * <code>ObjectFactory</code> must reside in the same package as the
     * expected return type in this case.
     * 
     * @param <T>
     *            the expected return type
     * @param fileName
     *            the name of the file to parse
     * @param contextPath
     *            the context path for JAXB
     * @return the root of the parsed document as the type specified
     * @throws FileNotFoundException
     *             if file not found
     * @throws JAXBException
     *             if parsing failed
     */
    public static <T> T parseXMLFile(String fileName, String contextPath)
            throws FileNotFoundException, JAXBException {
        FileReader fileReader = new FileReader(fileName);
        JAXBContext jc = JAXBContext.newInstance(contextPath);
        Unmarshaller u = jc.createUnmarshaller();

        @SuppressWarnings("unchecked")
        T xRoot = ((JAXBElement<T>) u.unmarshal(fileReader)).getValue();

        return xRoot;
    }
}

Related

  1. getJAXBContext(String packagePrefix)
  2. getJAXBContextForWebService(Class webService)
  3. getRequestContext()
  4. getRightsContext()
  5. isElement(String contextPath, Object object)
  6. toString(JAXBContext context, Object object)
  7. toXML(JAXBContext context, T obj)