package cn.com.origin.autocode.xmlutil;

/** 
 * @copyRight 浙江康勒科技有限公司
 * @author    作者 E-mail:hub@connor.net.cn 
 * @date      创建时间：2017-7-2 下午8:52:53 
 * @version   v1.0 
 * @parameter  
 * @since  
 * @return  
 */

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

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

public class CNXmlUtil {

	/**
	 * xml文件配置转换为对象
	 * 
	 * @param xmlPath
	 *            xml文件路径
	 * @param load
	 *            java对象.Class
	 * @return java对象
	 * @throws JAXBException
	 * @throws IOException
	 */
	public static Object xmlToBean(String xmlPath, Class<?> load)
			throws JAXBException, IOException {
		JAXBContext context = JAXBContext.newInstance(load);
		Unmarshaller unmarshaller = context.createUnmarshaller();
		Object object = unmarshaller.unmarshal(new File(xmlPath));
		return object;
	}

	public static void main(String[] args) throws IOException, JAXBException {
		String xmlPath = "C:\\Users\\Administrator\\AppData\\Local\\Temp\\20170702215339358.xml";
		Object object = CNXmlUtil.xmlToBean(xmlPath, CNPartList.class);
		CNPartList parts = (CNPartList) object;
		List<CNPart> partList = parts.getPartList();

		for (int i = 0; i < partList.size(); i++) {
			System.out.println(partList.get(i).getType());
			for (CNCompontent comp : partList.get(i).getCompontentList()) {
				System.out.println(comp.type);
				System.out.println(comp.compententType);
				for (CNProperty prop : comp.propertyList) {
					System.out.println(prop.displayName);
				}
			}
		}
		System.out.println("-----------------------------");
	}
}