[Simple-support] How to get an empty string, instead of null when reading @Text from element
Brought to you by:
niallg
|
From: Alexander P. <ale...@er...> - 2013-01-29 17:49:15
|
Hi
I have the following xml element:
<property name="my-prop"></property>
When I read it as in example below, the call to prop.getValue() returns null. How to change it to return empty string instead?
Sure, I can have a check in PropertyExample.getValue() method to check if the value is null then return ""; But was wondering if that is the recommended solution or if there is a better solution? Similar to @Text(empty="").
Rgards,
Alex
###################
package com.ericsson.commonlibrary.cf.xml.parser;
import java.io.File;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class Example {
public static void main(String[] args) throws Throwable {
File xml = new File("C:\\temp\\simple-xml\\property.xml"); // xml contains this text: <property name="my-prop"></property>
Serializer s = new Persister();
PropertyExample prop = s.read(PropertyExample.class, xml);
System.out.println(prop.getValue());
}
}
###################
package example;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Text;
@Root(name="property")
final class PropertyExample {
@Attribute(name="name") private String name;
@Text(required=false, empty="") private String value;
PropertyExample(@Attribute(name="name") String name, @Text(required=false, empty="") String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
}
###################
|