Android 中的简单库:“1”中的布尔值或“0”
简单的库很棒,我已经解析了很多 自过去 3 天以来,肥皂服务器的 XML 有所不同,但我遇到了 带有“0”或“1”的布尔属性:
<list mybool1="0" mybool2="1" attr1="attr" attr2="attr">
<page mybool3="1">
...
</page>
<page mybool3="0">
...
</page>
...
</list>
我尝试创建这个类:
public class Boolean01Converter implements Converter<Boolean>
{
@Override
public Boolean read(InputNode node) throws Exception {
return new Boolean(node.getValue().equals("1"));
}
@Override
public void write(OutputNode node, Boolean value) throws Exception {
node.setValue(value.booleanValue()?"1":"0");
}
}
并在我的对象定义上实现它:
@Root(name="list")
public class ListFcts
{
@Attribute
@Convert(Boolean01Converter.class)
private Boolean mybool1;
@Attribute
@Convert(Boolean01Converter.class)
private Boolean mybool2;
@Attribute
private int ...
@ElementList(name="page", inline=true)
private List<Page> pages;
public Boolean getMybool1() {
return mybool1;
}
}
但对于每个布尔值,我仍然得到 false。
[编辑]
事实上,当我这样做时:
@Override
public Boolean read(InputNode node) throws Exception {
return true;
}
我仍然得到 false
:
Serializer serial = new Persister();
ListFcts listFct = serial.read(ListFcts.class, soapResult);
if(listFct.getMybool1())
{
//this never happens
}else{
//this is always the case
}
所以我的转换器没有影响...
另外:我如何将转换器附加到 Persister 而不是 在 @Attributes 上声明它一百次?
非常感谢!
[edit2]
我放弃了 Converter,这是我自己的解决方案:
@Root(name="list")
public class ListFcts
{
@Attribute
private int mybool1;
@Attribute
private int mybool2;
public int getMybool1() {
return mybool1;
}
public Boolean isMybool1() {
return (mybool1==1)?true:false;
}
...
}
Simple library is great and i already parsed many
different XML from soap servers since last 3 days, but i encountered
boolean attributes with "0" or "1" :
<list mybool1="0" mybool2="1" attr1="attr" attr2="attr">
<page mybool3="1">
...
</page>
<page mybool3="0">
...
</page>
...
</list>
I tried to create this class :
public class Boolean01Converter implements Converter<Boolean>
{
@Override
public Boolean read(InputNode node) throws Exception {
return new Boolean(node.getValue().equals("1"));
}
@Override
public void write(OutputNode node, Boolean value) throws Exception {
node.setValue(value.booleanValue()?"1":"0");
}
}
and implemented it on my object definition :
@Root(name="list")
public class ListFcts
{
@Attribute
@Convert(Boolean01Converter.class)
private Boolean mybool1;
@Attribute
@Convert(Boolean01Converter.class)
private Boolean mybool2;
@Attribute
private int ...
@ElementList(name="page", inline=true)
private List<Page> pages;
public Boolean getMybool1() {
return mybool1;
}
}
But i still get false for every boolean.
[edit]
In fact, when i do this :
@Override
public Boolean read(InputNode node) throws Exception {
return true;
}
i still get false
for :
Serializer serial = new Persister();
ListFcts listFct = serial.read(ListFcts.class, soapResult);
if(listFct.getMybool1())
{
//this never happens
}else{
//this is always the case
}
so my Converter has no impact...
Also : how can I attach the converter to the Persister instead of
declaring it on @Attributes hundred times ?
Many thanks in advance !!
[edit2]
i give up with Converter, this is my own solution :
@Root(name="list")
public class ListFcts
{
@Attribute
private int mybool1;
@Attribute
private int mybool2;
public int getMybool1() {
return mybool1;
}
public Boolean isMybool1() {
return (mybool1==1)?true:false;
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码使用
node.getValue()
返回每个 XML 节点(示例中的“...”位)的值(读取:内容)。您需要的是读取属性值,例如
node.getAttributeValue("mybool1").equals("1")
Your code is using
node.getValue()
which returns the value (read: contents) of each XML node (the "..." bit in your examples).What you need is to reading the attribute values, something like
node.getAttributeValue("mybool1").equals("1")
我放弃了 Converter,我听说过 Transform 但没有找到如何使用它,所以这是我自己的基本解决方案:
i gave up with Converter, I heard about Transform but didn't find how to use it so this is my own basic solution :