xstream转换xml时出错
我的xml是这样的
`<?xml version="1.0" encoding="UTF-8"?> <MESSAGE>
<HEADER> <MESSAGENAME>CheckRequest</MESSAGENAME> <TRANSACTIONID>20201120110748404</TRANSACTIONID> <REPLYSUBJECTNAME>PROD.Listen.DRYT1200</REPLYSUBJECTNAME> <INBOXNAME /> <LISTENER>PEMListener</LISTENER> </HEADER>
<BODY> <LINENAME>DRYT0100</LINENAME> <LINE_RECIPE_LIST> <LINE_RECIPE> <RECIPEEVENTTYPE>Check</RECIPEEVENTTYPE> <LINERECIPENAME>E495A1A2400A1</LINERECIPENAME> <TIMEOUT_UNIT_LIST /> <CIMOFF_UNIT_LIST /> <UNIT_LIST> <UNITNAME>DRYT0141</UNITNAME> <UNITRECIPENAME>11</UNITRECIPENAME> <RECIPEPARALIST> <PARA> <TRACELEVEL>M</TRACELEVEL> <PARANAME>Recipe_Version</PARANAME> <VALUETYPE>NUMBER</VALUETYPE> <PARAVALUE>57.5A1B_NP_ETCH</PARAVALUE> </PARA> <PARA> <TRACELEVEL>M</TRACELEVEL> <PARANAME>LINE RECIPEID</PARANAME> <VALUETYPE>NUMBER</VALUETYPE> <PARAVALUE>0310</PARAVALUE> </PARA> </RECIPEPARALIST> </UNIT_LIST> </LINE_RECIPE> </LINE_RECIPE_LIST> <OPERATORID /> <TRANSACTIONSTARTTIME>2020-11-20 11:07:49.107</TRANSACTIONSTARTTIME> </BODY> </MESSAGE>`
我的class是这样的:
``
package com.example.rvxmldemo.testmdoel;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import java.util.ArrayList;
import java.util.List;
@XStreamAlias("MESSAGE")
public class Message {
@XStreamAlias("HEADER")
@XStreamAsAttribute
Header header;
@XStreamAlias("BODY")
@XStreamAsAttribute
Body body;
@XStreamAlias("RETURN")
@XStreamAsAttribute
Header.ReturnMessage Return;
public Message() {
}
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
public Header.ReturnMessage getReturn() {
return Return;
}
public void setReturn(Header.ReturnMessage aReturn) {
Return = aReturn;
}
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
@XStreamAlias("HEADER")
public class Header {
@XStreamAlias("MESSAGENAME")
@XStreamAsAttribute
String messageName;
@XStreamAlias("TRANSACTIONID")
@XStreamAsAttribute
String transactionID;
@XStreamAlias("REPLYSUBJECTNAME")
@XStreamAsAttribute
String replySubjectName;
@XStreamAlias("INBOXNAME")
@XStreamAsAttribute
String inboxName;
@XStreamAlias("LISTENER")
@XStreamAsAttribute
String listener;
public Header() {
}
@XStreamAlias("RETURN")
public class ReturnMessage {
@XStreamAlias("RETURNCODE")
String returnCode;
@XStreamAlias("RETURNMESSAGE")
String returnMessage;
public ReturnMessage() {
}
public String getReturnCode() {
return returnCode;
}
public void setReturnCode(String returnCode) {
this.returnCode = returnCode;
}
public String getReturnMessage() {
return returnMessage;
}
public void setReturnMessage(String returnMessage) {
this.returnMessage = returnMessage;
}
}
}
@XStreamAlias("BODY")
public class Body {
@XStreamAlias("LINENAME")
@XStreamAsAttribute
String lineName;
@XStreamAlias("LINE_RECIPE_LIST")
@XStreamImplicit(itemFieldName = "LINE_RECIPE")
List<LineRecipe> LINE_RECIPE_LIST;
@XStreamAlias("OPERATORID")
@XStreamAsAttribute
String operatorID;
@XStreamAlias("TRANSACTIONSTARTTIME")
@XStreamAsAttribute
String transactionStartTime;
public Body() {
getLineRecipeList();
}
public String getLineName() {
return lineName;
}
public void setLineName(String lineName) {
this.lineName = lineName;
}
public List<LineRecipe> getLineRecipeList() {
if (LINE_RECIPE_LIST == null) {
LINE_RECIPE_LIST = new ArrayList<>();
}
return LINE_RECIPE_LIST;
}
public void setLineRecipeList(List<LineRecipe> lineRecipeList) {
this.LINE_RECIPE_LIST = lineRecipeList;
}
public String getOperatorID() {
return operatorID;
}
public void setOperatorID(String operatorID) {
this.operatorID = operatorID;
}
public String getTransactionStartTime() {
return transactionStartTime;
}
public void setTransactionStartTime(String transactionStartTime) {
this.transactionStartTime = transactionStartTime;
}
}
@XStreamAlias("LINE_RECIPE")
public static class LineRecipe {
@XStreamAlias("RECIPEEVENTTYPE")
String recipeEventType;
@XStreamAlias("LINERECIPENAME")
String lineRecipeName;
@XStreamAlias("TIMEOUT_UNIT_LIST")
String TIMEOUT_UNIT_LIST;
@XStreamAlias("CIMOFF_UNIT_LIST")
String CIMOFF_UNIT_LIST;
@XStreamAlias("UNIT_LIST")
String UNIT_LIST;
public LineRecipe() {
// getUnitList();
}
public String getRecipeEventType() {
return recipeEventType;
}
public void setRecipeEventType(String recipeEventType) {
this.recipeEventType = recipeEventType;
}
public String getLineRecipeName() {
return lineRecipeName;
}
public void setLineRecipeName(String lineRecipeName) {
this.lineRecipeName = lineRecipeName;
}
// public List<TimeOutUnit> getTimeOutUnits() {
// if (TIMEOUT_UNIT_LIST == null) {
// TIMEOUT_UNIT_LIST = new ArrayList<>();
// }
// return TIMEOUT_UNIT_LIST;
// }
//
// public void setTimeOutUnits(List<TimeOutUnit> timeOutUnits) {
// this.TIMEOUT_UNIT_LIST = timeOutUnits;
// }
//
// public List<CimoffUnit> getCimoffUnits() {
// if (CIMOFF_UNIT_LIST == null) {
// CIMOFF_UNIT_LIST = new ArrayList<>();
// }
// return CIMOFF_UNIT_LIST;
// }
//
// public void setCimoffUnits(List<CimoffUnit> cimoffUnits) {
// this.CIMOFF_UNIT_LIST = cimoffUnits;
// }
// public List<Unit> getUnitList() {
// if (UNIT_LIST == null) {
// UNIT_LIST = new ArrayList<>();
// }
// return UNIT_LIST;
// }
//
// public void setUnitList(List<Unit> unitList) {
// this.UNIT_LIST = unitList;
// }
public String getTIMEOUT_UNIT_LIST() {
return TIMEOUT_UNIT_LIST;
}
public void setTIMEOUT_UNIT_LIST(String TIMEOUT_UNIT_LIST) {
this.TIMEOUT_UNIT_LIST = TIMEOUT_UNIT_LIST;
}
public String getCIMOFF_UNIT_LIST() {
return CIMOFF_UNIT_LIST;
}
public void setCIMOFF_UNIT_LIST(String CIMOFF_UNIT_LIST) {
this.CIMOFF_UNIT_LIST = CIMOFF_UNIT_LIST;
}
public String getUNIT_LIST() {
return UNIT_LIST;
}
public void setUNIT_LIST(String UNIT_LIST) {
this.UNIT_LIST = UNIT_LIST;
}
}
}
我的测试类是这样的:
``
package com.example.rvxmldemo.testmdoel;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.io.*;
public class Test12 {
public static void main(String[] args) {
String xml = "F:\\xml\\message.xml";
String s = readFile(xml);
Message message = toBean(s, Message.class);
System.out.println(message);
}
public static <T> T toBean(String xmlStr, Class<T> cls) {
XStream xstream = new XStream(new DomDriver());
xstream.setClassLoader(cls.getClassLoader());
xstream.processAnnotations(cls);
T obj = (T) xstream.fromXML(xmlStr);
return obj;
}
/**
* 一次性读取全部文件数据
*
* @param strFile
* @return
*/
public static String readFile(String strFile) {
String s = "";
try {
InputStream is = new FileInputStream(strFile);
int iAvail = is.available();
byte[] bytes = new byte[iAvail];
is.read(bytes);
s = new String(bytes);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
}
启动之后报错:
` Security framework of XStream not explicitly initialized, using predefined black list on your own risk. Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field com.example.rvxmldemo.testmdoel.Message$LineRecipe.LINE_RECIPE ---- Debugging information ---- message : No such field com.example.rvxmldemo.testmdoel.Message$LineRecipe.LINE_RECIPE field : LINE_RECIPE class : com.example.rvxmldemo.testmdoel.Message$LineRecipe required-type : com.example.rvxmldemo.testmdoel.Message$LineRecipe converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter path : /MESSAGE/BODY/LINE_RECIPE_LIST/LINE_RECIPE class[1] : com.example.rvxmldemo.testmdoel.Message$Body required-type[1] : com.example.rvxmldemo.testmdoel.Message$Body class[2] : com.example.rvxmldemo.testmdoel.Message required-type[2] : com.example.rvxmldemo.testmdoel.Message
version : 1.4.14
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.handleUnknownField(AbstractReflectionConverter.java:520) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:371) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:277) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:445) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:277) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:499) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:425) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:277) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134) at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32) at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1404) at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1383) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1268) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1259) at com.example.rvxmldemo.testmdoel.Test12.toBean(Test12.java:22) at com.example.rvxmldemo.testmdoel.Test12.main(Test12.java:12) Disconnected from the target VM, address: '127.0.0.1:52396', transport: 'socket'`
不知道是实体类哪里错了