当我使用 JAXB 编码器编码 java 对象时,编码器不会为 java 对象中的空文件创建空元素。例如,我有以下 java 对象:
public class PersonTraining {
@XmlElement(name = "Val1", required = true)
protected BigDecimal val1;
@XmlElement(name = "Val2", required = true, nillable = true)
protected BigDecimal val2;
@XmlElement(name = "Val3", required = true, nillable = true)
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar val3;
}
当我获取此对象的实例并将其编码到 XML 中时,我得到以下内容(这是因为我没有设置 Val2 的值):
<PersonTraining>
<Val1>1</Val1>
<Val3>2010-01-01T00:00:00.0-05:00</Val3>
</PersonTraining>
但是,我曾预料到编码操作会产生以下结果(事实上,我还特别需要元素,以便可以根据 XSD 验证 XML)
<PersonTraining>
<Val1>1</Val1>
<Val2></Val2>
<Val3>2010-01-01T00:00:00.0-05:00</Val3>
</PersonTraining>
请让我知道我需要设置什么选项,以便对象属性中的空值也可以被编码,并作为空/空元素返回。
这是编码代码:
StringWriter sw = new StringWriter();
JAXBContext jc = JAXBContext.newInstance("person_training");
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(ptl, sw);
最佳答案
默认一个JAXB (JSR-222)实现不会为空值编码(marshal)属性/元素。这将适用于您的 Java 模型中的以下字段。
@XmlElement(name = "Val1", required = true)
protected BigDecimal val1;
您可以通过在 @XmlElement 注释上指定 nillable=true 来覆盖此行为,就像您在此处所做的那样:
@XmlElement(name = "Val2", required = true, nillable = true)
protected BigDecimal val2;
这将使 xsi:nil="true" 属性发挥作用:
<Val2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
更多信息:
人员培训
由于您正在注释字段,因此您应该确保在类或包级别指定@XmlAccessorType(XmlAccessType.FIELD)(请参阅:http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html)。
import java.math.BigDecimal;
import javax.xml.bind.annotation.*;
import javax.xml.datatype.XMLGregorianCalendar;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class PersonTraining {
@XmlElement(name = "Val1", required = true)
protected BigDecimal val1;
@XmlElement(name = "Val2", required = true, nillable = true)
protected BigDecimal val2;
@XmlElement(name = "Val3", required = true, nillable = true)
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar val3;
}
演示
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(PersonTraining.class);
PersonTraining pt = new PersonTraining();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(pt, System.out);
}
}
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<personTraining>
<Val2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<Val3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</personTraining>
关于java - JAXB Marshaller 没有值为 null 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18983687/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle
查看我的Ruby代码:h=Hash.new([])h[0]=:word1h[1]=h[1]输出是:Hash={0=>:word1,1=>[:word2,:word3],2=>[:word2,:word3]}我希望有Hash={0=>:word1,1=>[:word2],2=>[:word3]}为什么要附加第二个哈希元素(数组)?如何将新数组元素附加到第三个哈希元素? 最佳答案 如果您提供单个值作为Hash.new的参数(例如Hash.new([]),完全相同的对象将用作每个缺失键的默认值。这就是您所拥有的,那是你不想要的。您可以改用
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我