如何使用XStream将默认值为XML的XML解组为空字符串?

时间:2017-09-16 04:53:59

标签: java scala xstream

我试图弄清楚如何在缺少标记时解组XML,我可以将默认值设置为空字符串而不是NULL。目前XStream使用null,这不是我想要的。

这个类有40多个属性,都是String。每个都有一个默认值的构造函数。我的意思是,像这样:

group = Hash.new 

def user_name
  puts "What is the name of this traveler?"
  group["name"]= gets.chomp.capitalize
end

def enter_expenses 
  puts "Welcome to the Expense Tracker System!\n".upcase
  puts "__________________________________________"
  puts "\nUse this system to track your group's expenses when traveling."
  print "Ready to get started? Enter yes to continue"
  ready_to_expense = gets.chomp.downcase


  4.times do 

    if ready_to_expense == "yes"
      puts "Welcome #{user_name}! Enter your expenses below:\n"

      puts "Amount spent on groceries:"
      group["groceries"]= gets.chomp.to_f

      puts "Amount spent on fuel & accommodations:"
      group["fuel_and_accommodations"]= gets.chomp.to_f

      puts "Amount spent recreational activities:"
      group["recreational_activities"] = gets.chomp.to_f

    elsif "Please come back when ready to enter your expenses."
    end
  end
end

enter_expenses
create_travelers

puts "__________________________________________"
puts "Thanks for using the expense tracker system!".upcase

(是的,我正在尝试将它与Scala一起使用)

从技术上讲,我可以编写一个自定义转换器,将它们设置为空字符串,但这感觉有点傻。

我尝试使用此

/dist/ngfactory/node_modules/@angular/material/typings/index.ngfactory.ts:9
import * as i0 from '@angular/core';
^^^^^^

SyntaxError: Unexpected token import

从此处建议:https://packagecontrol.io/packages/ColdFusion

它似乎不起作用。

还有其他想法吗?

1 个答案:

答案 0 :(得分:0)

XStreams使用默认的空构造函数,然后通过调用setter来跟进它。因此,为了在没有自定义转换器的情况下使其工作,您将需要创建一个显式空构造函数,该构造函数使用您期望的默认值填充所有内容。这是一个示例工作应用程序:

import com.thoughtworks.xstream.XStream
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider

object Hello extends App {
  val xstream = new XStream(new PureJavaReflectionProvider())
  xstream.alias("MyData", classOf[MyData])
  val output = xstream.fromXML("<MyData><id>This should fill in</id></MyData>")
  println(output)
}

case class MyData(id: String = "", var b: String = "") 
{
   def this() = this("", "")
}