[英]How to remove space from xml tag using XStream?
I have the xml below that has a number value 我下面的xml具有数字值
<detalhe>
<numLinha>1048 </numLinha>
</detalhe
the tag numLinha will be converted to a number attibute in java, however i receive the folow message because there are spaces at the end of tag: 标签numLinha将在Java中转换为数字attibute,但是我收到以下消息,因为标签末尾有空格:
Exception in thread "main"
com.thoughtworks.xstream.converters.ConversionException: For input string "1048 "
@XStreamAlias("detalhe")
public class BodyDetail implements Serializable {
private static final long serialVersionUID = -1000785985130865301L;
@XStreamAlias("numLinha")
private Integer lineNumber;
}
I solve my problem with a custom converter of Integer Type. 我使用整数类型的自定义转换器解决了我的问题。
First i created the converter: 首先,我创建了转换器:
package converter; import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.converters.UnmarshallingContext; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; public class IntegerConverter implements Converter { @SuppressWarnings("rawtypes") @Override public boolean canConvert(Class clazz) { return clazz.equals(Integer.class); } @Override public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingContext context) { } @Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { String text = (String)reader.getValue(); Integer number = Integer.parseInt(text.trim()); return number; } }
I registered the converter: 我注册了转换器:
public class XMLRead { public static void main(String[] args) { XStream xStream = new XStream(); xStream.registerConverter(new IntegerConverter()); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.