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

huangapple go评论108阅读模式
英文:

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 文件:

  1. <com.baeldung.pojo.Customer>
  2. <firstName>John</firstName>
  3. <lastName>Doe</lastName>
  4. <dob>1986-02-14 03:46:16.381 UTC</dob>
  5. </com.baeldung.pojo.Customer>

这是 Java 对象:

  1. public class Customer {
  2. private String firstName;
  3. private String lastName;
  4. private Date dob;
  5. private isMale;
  6. // 标准的设置器和获取器(setters and getters)
  7. }

在 XML 文件中没有 isMale,所以当我将 XML 文件转换为 Customer 对象时,它会将 isMale 赋值为 false,但我希望它默认只赋值一次为 true。

转换代码:

  1. public static <A> A fromXML(Class<A> c, File file) {
  2. XStream xStream = new XStream();
  3. // 避免出现“XStream 安全框架未初始化,XStream 可能存在漏洞”的警告。
  4. XStream.setupDefaultSecurity(xStream);
  5. xStream.allowTypesByWildcard(new String[] {
  6. "configurationFiles.**",
  7. "java.awt.Dimension"
  8. });
  9. xStream.ignoreUnknownElements();
  10. InputStream inputStream = null;
  11. Reader reader = null;
  12. try {
  13. inputStream = new java.io.FileInputStream(file);
  14. //System.out.println("inside fromXml try");
  15. reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
  16. Object object = xStream.fromXML(reader);
  17. // boolean isMarriedExists = ((Object) xStream).getUnmarshaller().getContext().hasField("isMarried");
  18. // System.out.println(c+"--"+object);
  19. if (c.isInstance(object)) {
  20. return (A)object;
  21. }
  22. }
  23. catch (Exception exp) {
  24. //System.out.println("inside fromXml catch");
  25. exp.printStackTrace();
  26. //log.error(null, exp);
  27. } catch (java.lang.Error err) {
  28. //System.out.println("inside fromXml catch");
  29. err.printStackTrace();
  30. //log.error(null, err);
  31. } finally {
  32. close(reader);
  33. close(inputStream);
  34. }
  35. return null;
  36. }

使用上述函数并使用 xStream 转换 XML 文件:

  1. File f = new File("customer.xml");
  2. MainFrameXmlOptions mainFrameOptions=null;
  3. if(f.exists())
  4. mainFrameOptions = fromXML(MainFrameXmlOptions.class, f);
英文:

this is xml file

  1. <com.baeldung.pojo.Customer>
  2. <firstName>John</firstName>
  3. <lastName>Doe</lastName>
  4. <dob>1986-02-14 03:46:16.381 UTC</dob>
  5. </com.baeldung.pojo.Customer>

this is java object

  1. public class Customer {
  2. private String firstName;
  3. private String lastName;
  4. private Date dob;
  5. private isMale;
  6. // standard setters and getters
  7. }

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

  1. public static <A> A fromXML(Class<A> c, File file) {
  2. XStream xStream = new XStream();
  3. // Avoid "Security framework of XStream not initialized, XStream is probably vulnerable." warning.
  4. XStream.setupDefaultSecurity(xStream);
  5. xStream.allowTypesByWildcard(new String[] {
  6. "configurationFiles.**",
  7. "java.awt.Dimension"
  8. });
  9. xStream.ignoreUnknownElements();
  10. InputStream inputStream = null;
  11. Reader reader = null;
  12. try {
  13. inputStream = new java.io.FileInputStream(file);
  14. //System.out.println("inside fromXml try");
  15. reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
  16. Object object = xStream.fromXML(reader);
  17. // boolean isMarriedExists = ((Object) xStream).getUnmarshaller().getContext().hasField("isMarried");
  18. // System.out.println(c+"--"+object);
  19. if (c.isInstance(object)) {
  20. return (A)object;
  21. }
  22. }
  23. catch (Exception exp) {
  24. //System.out.println("inside fromXml catch");
  25. exp.printStackTrace();
  26. //log.error(null, exp);
  27. } catch (java.lang.Error err) {
  28. //System.out.println("inside fromXml catch");
  29. err.printStackTrace();
  30. //log.error(null, err);
  31. } finally {
  32. close(reader);
  33. close(inputStream);
  34. }
  35. return null;
  36. }

using above mention function and converting the xml file using xStream.

  1. File f = new File("customer.xml");
  2. MainFrameXmlOptions mainFrameOptions=null;
  3. if(f.exists())
  4. mainFrameOptions = fromXML(MainFrameXmlOptions.class, f);

答案1

得分: 1

尝试添加构造函数

  1. public Customer() {
  2. super();
  3. isMale = true;
  4. }

您需要使用以下代码使XStream遵守这个构造函数

  1. XStream xStream = new XStream(new com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider());
英文:

Try adding ctor

  1. public Customer() {
  2. super();
  3. isMale = true;
  4. }

You will need to get XStream to honour that with

  1. XStream xStream = new XStream(new com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider());

huangapple
  • 本文由 发表于 2023年7月20日 19:08:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定