3

シンプルなライブラリは素晴らしく、私は過去 3 日間から SOAP サーバーからさまざまな 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 になります。

[edit] 実際、私がこれを行うとき:

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

だから私のコンバーターは影響を与えません...

また、 @Attributes で何百回も宣言する代わりに、コンバーターを Persister にアタッチするにはどうすればよいですか?

よろしくお願いします!!

[edit2]

私はコンバーターをあきらめます、これは私自身の解決策です:

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

    ...
}
4

2 に答える 2

2

あなたのコードは、各XMLノードの(読み取り:コンテンツ)node.getValue()を返すものを使用しています(例の「...」ビット)。

必要なのは、次のような属性値を読み取ることですnode.getAttributeValue("mybool1").equals("1")

于 2011-09-30T12:35:47.490 に答える
0

私はコンバーターをあきらめました、私は変換について聞いたが、それを使用する方法を見つけられなかったので、これは私自身の基本的な解決策です:

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

    ...
}
于 2011-10-10T08:57:32.457 に答える