[英]SimpleXML framework: How to convert a non-standard basic type representation?
I am trying to convince SimpleXML to convert my Java objects to and from XML. 我试图说服SimpleXML将我的Java对象与XML相互转换。 I have a (seemingly) simple problem but I have now already wasted substantial time searching a description or an example for doing the following:
我有一个(看似)简单的问题,但是现在我已经浪费了大量时间来搜索描述或示例,以进行以下操作:
The XML format that I need to read or generate contains integer values that define colors and which are represented as hex integers (but without a leading "0x", same as in HTML or in misc. Android .xml files), ie red="ff0000", blue="00ff00", green="0000ff", etc. 我需要读取或生成的XML格式包含定义颜色的整数值,这些整数值表示为十六进制整数(但没有前导“ 0x”,与HTML或其他Android .xml文件中的相同),即red =“ ff0000“,blue =” 00ff00“,green =” 0000ff“等。
My XML contains elements like: 我的XML包含以下元素:
<SomeObject name="foobar" checkedColor="123456" flaggedColor="FEDCBA" ... />
The corresponding Java class reads: 相应的Java类读取:
public class SomeObject
{
@Attribute
String name;
@Attribute #######
int checkedColor;
@Attribute #######
int flaggedColor;
// ...
}
I marked the two attributes I am talking about here with ####### above. 我用上面的########标记了我在这里谈论的两个属性。 The color values should be of type "int".
颜色值应为“ int”类型。
How do I teach SimpleXML that these value are represented as hex-string and how to convert them? 如何教SimpleXML将这些值表示为十六进制字符串,以及如何转换它们? What transformer or converter or whatever do I need to write so that these int values are converted to/from hex-strings as shown and what do I have to annotate in the above code to achieve that?
为了使这些int值转换为十六进制字符串或从中转换为十六进制字符串,我需要编写什么转换器或转换器,或者需要在上面的代码中添加哪些注释才能实现?
Use a Transform, see the documentation below 使用转换,请参见下面的文档
http://simple.sourceforge.net/download/stream/doc/javadoc/org/simpleframework/xml/transform/Transform.html http://simple.sourceforge.net/download/stream/doc/javadoc/org/simpleframework/xml/transform/Transform.html
I was finally able to implement what I want by defining a type "HexInt" which looks as follows 我终于可以通过定义如下所示的“ HexInt”类型来实现所需的功能
public class HexInt
{
private int value;
public HexInt(int value) {
setValue(value);
}
public HexInt(String value) {
setValue(value);
}
protected int getValue() {
return this.value;
}
protected void setValue(int value) {
this.value = value;
}
protected void setValue(String value) {
setValue(Integer.parseInt(value, 16));
}
@Override
public String toString() {
return Integer.toHexString(getValue());
}
}
and defining the corresponding attributes as 并将相应的属性定义为
...
@Attribute(required=false)
protected HexInt checkedcolor;
@Attribute(required=false)
protected HexInt flaggedcolor;
...
plus defining a Matcher which for HexInt-s provides a corresponding Transformer that properly converts these values to and from strings. 加上定义一个Matcher,它为HexInt-s提供了一个相应的Transformer,可以正确地将这些值与字符串进行转换。
But what I hate about this solution is, that I not only had to annotate my java code to achieve that result but actually had to use special, representation-specific classes, even though from the program logic there would be absolutely no need to define a special class containing only an int, just because I want to serialize these two fields in a special way. 但是,我讨厌这种解决方案的地方是,我不仅必须注释我的Java代码以实现该结果,而且实际上必须使用特殊的,表示特定的类,即使从程序逻辑上绝对不需要定义一个仅包含一个int的特殊类,仅因为我想以特殊方式序列化这两个字段。 IMHO following an aspect oriented spirit the internal representation ("int") should be defined in the basic Java-code and that code should be agnostic of how the data gets represented when externalized.
恕我直言,我将遵循面向方面的精神,在基本的Java代码中定义内部表示(“ int”),并且该代码应与外部化时如何表示数据无关。 The external representation on the other hand (ie whether I want to represent int-value as a decimal or hexadecimal string) should be defined in the annotations ONLY (plus supportive classes like Matcher and Transformer) but not in the basic Java code.
另一方面,外部表示(即,我要将int值表示为十进制还是十六进制字符串)应仅在注释中(加上支持类,例如Matcher和Transformer)定义,而不能在基本Java代码中定义。
Is there really no way in the SimpleXML-framework to keep these different aspects more cleanly separated? 在SimpleXML框架中,真的没有办法将这些不同的方面更清楚地分开吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.