开发者

Simple library in Android : Boolean in "1" or "0"

开发者 https://www.devze.com 2023-04-10 06:42 出处:网络
Simple library is great and i already parsed many different XML from soap servers since last 3 days, but i encountered

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 mybo开发者_如何学Gool1;
    }
}

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

    ...
}


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")


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

    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号