英文:
converting xml file into Object using xstream but if a Variable not present in xml file but in Object then it assign default value to it how to change
问题
这是 XML 文件:
<com.baeldung.pojo.Customer>
<firstName>John</firstName>
<lastName>Doe</lastName>
<dob>1986-02-14 03:46:16.381 UTC</dob>
</com.baeldung.pojo.Customer>
这是 Java 对象:
public class Customer {
private String firstName;
private String lastName;
private Date dob;
private isMale;
// 标准的设置器和获取器(setters and getters)
}
在 XML 文件中没有 isMale,所以当我将 XML 文件转换为 Customer 对象时,它会将 isMale 赋值为 false,但我希望它默认只赋值一次为 true。
转换代码:
public static <A> A fromXML(Class<A> c, File file) {
XStream xStream = new XStream();
// 避免出现“XStream 安全框架未初始化,XStream 可能存在漏洞”的警告。
XStream.setupDefaultSecurity(xStream);
xStream.allowTypesByWildcard(new String[] {
"configurationFiles.**",
"java.awt.Dimension"
});
xStream.ignoreUnknownElements();
InputStream inputStream = null;
Reader reader = null;
try {
inputStream = new java.io.FileInputStream(file);
//System.out.println("inside fromXml try");
reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
Object object = xStream.fromXML(reader);
// boolean isMarriedExists = ((Object) xStream).getUnmarshaller().getContext().hasField("isMarried");
// System.out.println(c+"--"+object);
if (c.isInstance(object)) {
return (A)object;
}
}
catch (Exception exp) {
//System.out.println("inside fromXml catch");
exp.printStackTrace();
//log.error(null, exp);
} catch (java.lang.Error err) {
//System.out.println("inside fromXml catch");
err.printStackTrace();
//log.error(null, err);
} finally {
close(reader);
close(inputStream);
}
return null;
}
使用上述函数并使用 xStream 转换 XML 文件:
File f = new File("customer.xml");
MainFrameXmlOptions mainFrameOptions=null;
if(f.exists())
mainFrameOptions = fromXML(MainFrameXmlOptions.class, f);
英文:
this is xml file
<com.baeldung.pojo.Customer>
<firstName>John</firstName>
<lastName>Doe</lastName>
<dob>1986-02-14 03:46:16.381 UTC</dob>
</com.baeldung.pojo.Customer>
this is java object
public class Customer {
private String firstName;
private String lastName;
private Date dob;
private isMale;
// standard setters and getters
}
isMale not in xml file so when i convert xml file into Customer Object it assign isMale=false but i want it to assign it true by default only for once
conversion code
public static <A> A fromXML(Class<A> c, File file) {
XStream xStream = new XStream();
// Avoid "Security framework of XStream not initialized, XStream is probably vulnerable." warning.
XStream.setupDefaultSecurity(xStream);
xStream.allowTypesByWildcard(new String[] {
"configurationFiles.**",
"java.awt.Dimension"
});
xStream.ignoreUnknownElements();
InputStream inputStream = null;
Reader reader = null;
try {
inputStream = new java.io.FileInputStream(file);
//System.out.println("inside fromXml try");
reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
Object object = xStream.fromXML(reader);
// boolean isMarriedExists = ((Object) xStream).getUnmarshaller().getContext().hasField("isMarried");
// System.out.println(c+"--"+object);
if (c.isInstance(object)) {
return (A)object;
}
}
catch (Exception exp) {
//System.out.println("inside fromXml catch");
exp.printStackTrace();
//log.error(null, exp);
} catch (java.lang.Error err) {
//System.out.println("inside fromXml catch");
err.printStackTrace();
//log.error(null, err);
} finally {
close(reader);
close(inputStream);
}
return null;
}
using above mention function and converting the xml file using xStream.
File f = new File("customer.xml");
MainFrameXmlOptions mainFrameOptions=null;
if(f.exists())
mainFrameOptions = fromXML(MainFrameXmlOptions.class, f);
答案1
得分: 1
尝试添加构造函数
public Customer() {
super();
isMale = true;
}
您需要使用以下代码使XStream
遵守这个构造函数
XStream xStream = new XStream(new com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider());
英文:
Try adding ctor
public Customer() {
super();
isMale = true;
}
You will need to get XStream
to honour that with
XStream xStream = new XStream(new com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论