Android 中的简单库:“1”中的布尔值或“0”

发布于 2024-12-07 16:15:19 字数 2032 浏览 0 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-12-14 16:15:19

您的代码使用 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")

乜一 2024-12-14 16:15:19

我放弃了 Converter,我听说过 Transform 但没有找到如何使用它,所以这是我自己的基本解决方案:

@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;
    }

    ...
}

i gave up with Converter, I heard about Transform but didn't find how to use it so this is my own basic 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;
    }

    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文