当前位置 : 主页 > 网页制作 > xml >

Xml转换Java Bean

来源:互联网 收集:自由互联 发布时间:2021-06-13
工具类 /** * 将XML转为指定的POJO * @param clazz * @param xmlStr * @return * @throws Exception */ public static Object xmlStrToOject(Class? clazz, String xmlStr) throws Exception { Object xmlObject = null; Reader reader = null; try{
工具类
/**
* 将XML转为指定的POJO
* @param clazz
* @param xmlStr
* @return
* @throws Exception
*/
public static Object xmlStrToOject(Class<?> clazz, String xmlStr) throws Exception {
Object xmlObject = null;
Reader reader = null;
try{
JAXBContext context = JAXBContext.newInstance(clazz);

// XML 转为对象的接口
Unmarshaller unmarshaller = context.createUnmarshaller();

reader = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(reader);

if (null != reader) {
reader.close();
}
}catch (Exception e){e.printStackTrace();}


return xmlObject;
}

使用工具类
Vehicle vehicle = (Vehicle)XmlUtil.xmlStrToOject(Vehicle.class,xmlStr);

String str = vehicle.getCamID().get(0);
测试
public static void main(String[] args) {
String xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"\n" +
"<Vehicle> \n" +
" <CamID>43030412341319100034</CamID> \n" +
" <CarPlate>湘X538D警</CarPlate> \n" +
" <PlateColor>0</PlateColor> \n" +
" <VehicleSpeed>1</VehicleSpeed> \n" +
" <PlateType>23</PlateType> \n" +
" <IdentifyStatus>0</IdentifyStatus> \n" +
" <VehicleColor>Z</VehicleColor> \n" +
" <VehicleType>1</VehicleType> \n" +
" <DriveStatus>12080</DriveStatus> \n" +
" <TollgateID>KKBM43030412341319100034</TollgateID> \n" +
" <PassTime>20190926172608099</PassTime> \n" +
" <RecordID>979799083</RecordID> \n" +
" <DBRecordID>0</DBRecordID> \n" +
" <Image> \n" +
" <ImageIndex>1</ImageIndex> \n" +
" <ImageURL>http://43.27.0.85:8086/43.27.0.91:7549/alarm$762143830_91532/2019/09/26/17/KKBM43030412341319100034/260891900.jpg?dev=XT8Q-CDS-1&amp;fid=117508-29-13D2E8002D-155A0BA-63B56</ImageURL> \n" +
" <PassTime>20190926172601791</PassTime> \n" +
" <ImageType>1</ImageType> \n" +
" <ImageData/> \n" +
" </Image> \n" +
" <Image> \n" +
" <ImageIndex>2</ImageIndex> \n" +
" <ImageURL>http://43.27.0.85:8086/43.27.0.91:7549/alarm$762143830_91532/2019/09/26/17/KKBM43030412341319100034/260891901.jpg?dev=XT8Q-CDS-1&amp;fid=117508-29-13D2E8002D-15BDCD8-5ECB4</ImageURL> \n" +
" <PassTime>20190926172603929</PassTime> \n" +
" <ImageType>1</ImageType> \n" +
" <ImageData/> \n" +
" </Image> \n" +
" <Image> \n" +
" <ImageIndex>3</ImageIndex> \n" +
" <ImageURL>http://43.27.0.85:8086/43.27.0.91:7549/alarm$762143830_91532/2019/09/26/17/KKBM43030412341319100034/260891902.jpg?dev=XT8Q-CDS-1&amp;fid=117508-29-13D2E8002D-161CA54-4FF99</ImageURL> \n" +
" <PassTime>20190926172608099</PassTime> \n" +
" <ImageType>1</ImageType> \n" +
" <ImageData/> \n" +
" </Image> \n" +
" <Image> \n" +
" <ImageIndex>4</ImageIndex> \n" +
" <ImageURL>http://43.27.0.85:8086/43.27.0.91:7549/alarm$762143830_91532/2019/09/26/17/KKBM43030412341319100034/Character260891903.jpg?dev=XT8Q-CDS-1&amp;fid=117508-29-13D2E8002D-166CAB5-78E</ImageURL> \n" +
" <ImageType>2</ImageType> \n" +
" <ImageData/> \n" +
" </Image> \n" +
" <PicNumber>4</PicNumber> \n" +
" <LaneID>1</LaneID> \n" +
" <LaneType>1</LaneType> \n" +
" <Direction>1</Direction> \n" +
" <VehicleBrand>98</VehicleBrand> \n" +
" <PlateNumber>1</PlateNumber> \n" +
" <VehicleBody/> \n" +
" <VehicleLength>0</VehicleLength> \n" +
" <DealTag/> \n" +
" <PlaceCode>GXCY</PlaceCode> \n" +
" <EquipmentType>1</EquipmentType> \n" +
" <PlateConfidence>78</PlateConfidence> \n" +
" <RearPlateConfidence>0</RearPlateConfidence> \n" +
" <GlobalComposeFlag>3</GlobalComposeFlag> \n" +
" <RedLightStartTime/> \n" +
" <RedLightEndTime/> \n" +
" <RedLightTime>0</RedLightTime> \n" +
" <LimitedSpeed>70</LimitedSpeed> \n" +
" <MarkedSpeed>70</MarkedSpeed> \n" +
" <VideoURL/> \n" +
" <VideoURL2/> \n" +
" <VehicleTopX>0000</VehicleTopX>\n" +
" <VehicleTopY>0800</VehicleTopY>\n" +
" <VehicleBotX>0568</VehicleBotX>\n" +
" <VehicleBotY>1558</VehicleBotY>\n" +
" <LPRRectTopX>0166</LPRRectTopX>\n" +
" <LPRRectTopY>1298</LPRRectTopY>\n" +
" <LPRRectBotX>0244</LPRRectBotX>\n" +
" <LPRRectBotY>1318</LPRRectBotY>\n" +
" <VehicleFace> \n" +
" <VehicleBrand/> \n" +
" <VehicleBrandType/> \n" +
" <VehicleBrandYear/> \n" +
" <VehicleBrandModel/> \n" +
" <IsVehicleHead/> \n" +
" </VehicleFace> \n" +
" <PlaceName>road</PlaceName> \n" +
" <PoliceCode/> \n" +
" <ReservedField1/> \n" +
" <ReservedField2/> \n" +
" <VehicleColorDept>0</VehicleColorDept> \n" +
" <TollgateName>高新路创业路-东向西-电警</TollgateName> \n" +
" <DressColor/> \n" +
" <ApplicationType/> \n" +
" <RearPlateColor/> \n" +
" <RearPlateType/> \n" +
" <RearVehiclePlateID/> \n" +
" <PlateCoincide/> \n" +
" <DirectionName/> \n" +
" <CamID/> \n" +
" <ImageURL2/> \n" +
" <ImageURL3/> \n" +
" <ImageURL4/> \n" +
" <TollgateName/> \n" +
"</Vehicle>";

System.out.println("xml数据="+xmlStr);
//转换xml
Vehicle vehicle = null;
try {
//vehicle = changXmlOfPassCar(xmlStr);
vehicle = (Vehicle)XmlUtil.xmlStrToOject(Vehicle.class,xmlStr);
log.info("号牌种类为:{},车辆类型为:{}", vehicle.getPlateType(), vehicle.getVehicleType());
} catch (Exception e) {
e.printStackTrace();
}
}

/**过车bean
* Created by mW6408 on 2019/4/10.
*/
@Data
@XmlAccessorType(value= XmlAccessType.FIELD)
@XmlRootElement(name="Vehicle")
public class Vehicle {
private List<String> CamID; //卡口相机号
private String CarPlate;//号牌号码
private String PlateColor;//号牌颜色
private String VehicleSpeed;//车辆速度
private String PlateType;//号牌种类
private String IdentifyStatus;
private String VehicleColor;//车身颜色
private String VehicleType;//车辆类型
private String DriveStatus;//违法行为
private String TollgateID;//卡口编号
private String PassTime;//违法时间
private String RecordID;
private String DBRecordID;
private List<Image> Image;
private String PicNumber;//照片数量
private String LaneID;//车道编号
private String LaneType;
private String Direction;//方向编码
private String VehicleBrand;//车辆分类
private String PlateNumber;
private String VehicleBody;
private String VehicleLength;
private String DealTag;
private String PlaceCode;//违法地点
private String EquipmentType;
private String PlateConfidence;
private String RearPlateConfidence;
private String GlobalComposeFlag;
private String RedLightStartTime;
private String RedLightEndTime;
private String RedLightTime;
private String LimitedSpeed;
private String MarkedSpeed;
private String VideoURL;
private String VideoURL2;
private String VehicleTopX;
private String VehicleTopY;
private String VehicleBotX;
private String VehicleBotY;
private String LPRRectTopX;
private String LPRRectTopY;
private String LPRRectBotX;
private String LPRRectBotY;
private VehicleFace VehicleFace;
private String PlaceName;//违法地址
private String PoliceCode;
private String ReservedField1;
private String ReservedField2;
private String VehicleColorDept;
private String DressColor;
private String ApplicationType;
private String RearPlateColor;
private String RearPlateType;
private String RearVehiclePlateID;
private String PlateCoincide;
private String DirectionName;
private String ImageURL2;
private String ImageURL3;
private String ImageURL4;
private String TollgateName;
}

/**
* Created by liuzhou on 2019/9/29.
*/
@Data
@XmlAccessorType(value= XmlAccessType.FIELD)
@XmlRootElement(name = "Image")
public class Image {
private String ImageIndex;
private String ImageURL;
private String PassTime;
private String ImageType;
private String ImageData;
}

/**
* Created by liuzhou on 2019/9/29.
*/
@Data
@XmlAccessorType(value= XmlAccessType.FIELD)
public class VehicleFace {
private String VehicleBrand;
private String VehicleBrandType;
private String VehicleBrandYear;
private String VehicleBrandModel;
private String IsVehicleHead;
}

参考:

   https://blog.csdn.net/LookForDream_/article/details/88884316

   https://zhuchengzzcc.iteye.com/blog/1838702

网友评论