package com.inspect.base.core.utils.xml;

import java.io.Reader;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

public class XmlToBean {
    public static Object xmlStrToObject(Class<?> clazz, String xmlStr) throws Exception {
        Object xmlObject = null;
        Reader reader = null;
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        reader = new StringReader(xmlStr);
        xmlObject = unmarshaller.unmarshal(reader);
        if (null != reader) {
            reader.close();
        }

        return xmlObject;
    }

    public static <T> T convertToJavaBean(String xml, Class<T> c) {
        Object t = null;

        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            t = unmarshaller.unmarshal(new StringReader(xml));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return (T) t;
    }

    public static void main(String[] args) throws Exception {
        String xml = "<SIP_XML EventType=\"Push_Resourse\">\n    <Code name=\"端机控制台\" id=\"888\" />\n    <SubList>\n        <Item Code=\"1801004\" Name=\"吉山\" DecoderTag=\"\" Longitude=\"\" Latitude=\"\" SubNum=\"3\" OnLine=\"1\">\n            <SubList SubNum=\"3\">\n                <Item Name=\"192.168.1.41,6351\" Code=\"4\" type=\"ir\" DecoderTag=\"\" Longitude=\"\" Latitude=\"\" SubNum=\"0\" OnLine=\"1\" url_ir=\"10.10.18.182:554\" videoWidth=\"640\" videoHeight=\"480\" />\n                <Item Name=\"IPC1005\" Code=\"1200914015\" type=\"vl\" DecoderTag=\"\" Longitude=\"\" Latitude=\"\" SubNum=\"0\" OnLine=\"1\" videoWidth=\"1280\" videoHeight=\"720\" url_vl=\"10.10.18.182:554\" />\n                <Item Name=\"192.168.1.65,6351\" Code=\"1\" type=\"ptz\" DecoderTag=\"\" Longitude=\"\" Latitude=\"\" SubNum=\"0\" OnLine=\"1\" />\n            </SubList>\n        </Item>\n    </SubList>\n</SIP_XML>";
        String xmla = "<SIP_XML EventType=\"Pulse\">\n    <Code url=\"10.10.18.121:9056\" name=\"吉山\" id=\"1801004\" role=\"3\" />\n</SIP_XML>";
        String pointModel = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n<Device_Model> \n<Items>\n<Item \nstation_name=\"变电站名称\" \nstation_id=\"变电站编码\" \narea_id=\"区域 ID\" \narea_name=\"区域名称\" \nbay_id=\"间隔 ID\"\nbay_name=\"间隔名称\" \nmain_device_id=\"主设备 ID\" \nmain_device_name=\"主设备名称\" \ncomponent_id=\"部件 ID\" \ncomponent_name=\"部件名称\" \ndevice_id=\"设备点位 ID\" \ndevice_name=\"设备点位名称\" \ndevice_type=\"主设备类型\" \nmeter_type=\"表计类型\"\nappearance_type=\"辅助设施类型\" \nsave_type_list=\"采集/保存文件类型列表\" \nrecognition_type_list=\"识别类型列表\" \nphase=\"相位\" \ndevice_info=\"备注信息\" \ndata_type =\"设备点位支持的数据来源\" \nlower_value =\"正常范围下限\" \nupper_value =\"正常范围上限\" \nvideo_pos =\"关联视频编码及预置位\" /> \n</Items>\n<Device_Model> \n<Items>\n<Item \nstation_name=\"变电站名称\" \nstation_id=\"变电站编码\" \narea_id=\"区域 ID\" \narea_name=\"区域名称\" \nbay_id=\"间隔 ID\"\nbay_name=\"间隔名称\" \nmain_device_id=\"主设备 ID\" \nmain_device_name=\"主设备名称\" \ncomponent_id=\"部件 ID\" \ncomponent_name=\"部件名称\" \ndevice_id=\"设备点位 ID\" \ndevice_name=\"设备点位名称\" \ndevice_type=\"主设备类型\" \nmeter_type=\"表计类型\"\nappearance_type=\"辅助设施类型\" \nsave_type_list=\"采集/保存文件类型列表\" \nrecognition_type_list=\"识别类型列表\" \nphase=\"相位\" \ndevice_info=\"备注信息\" \ndata_type =\"设备点位支持的数据来源\" \nlower_value =\"正常范围下限\" \nupper_value =\"正常范围上限\" \nvideo_pos =\"关联视频编码及预置位\" /> \n</Items>\n</Device_Model>";
    }
}
