2

JAXBを適切に印刷xmlns:xsixsi:nillてnillableにする方法はあります@XmlRootElementか?

public class XmlValueTest {

    public static void main(final String[] args) throws JAXBException {

        final JAXBContext context =
            JAXBContext.newInstance(Wrapper.class, Value.class);

        final Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        marshaller.marshal(Value.newInstance(null), System.out);
        marshaller.marshal(Value.newInstance("null"), System.out);
        marshaller.marshal(Wrapper.newInstance(null), System.out);
        marshaller.marshal(Wrapper.newInstance("null"), System.out);
    }
}

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
class Value {

    public static Value newInstance(final String raw) {
        final Value instance = new Value();
        instance.raw = raw;
        return instance;
    }

    @XmlValue
    private String raw;
}

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
class Wrapper {

    public static Wrapper newInstance(final String raw) {
        final Wrapper wrapper = new Wrapper();
        wrapper.raw = raw;
        return wrapper;
    }

    @XmlElement(nillable = true, required = true)
    private String raw;
}

プリント

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<value/> <!-- is this normal? -->

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<value>null</value>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<wrapper>
    <raw xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</wrapper>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<wrapper>
    <raw>null</raw>
</wrapper>

私はただ最初にとで<value/>武装させる方法があるか知りたいです。xmlns:xsixsi:nill

4

1 に答える 1

1

注: 私はEclipseLink JAXB(MOXy)のリーダーであり、JAXB(JSR-222)エキスパートグループのメンバーです。

標準のJAXBAPIを使用してこれを行う方法があるとは思いません。@XmlElement(nillable=true)以下は、を活用し@XmlPath("text()")て目的の動作を取得することで実行できる例です。

価値

package forum11796699;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
public class Value {

    private String value;

    @XmlElement(nillable=true)
    @XmlPath("text()")
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

jaxb.properties

MOXyをJAXBプロバイダーとして指定するにはjaxb.properties、ドメインモデルと同じパッケージで次のエントリを含むファイルを呼び出す必要があります( http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-asを参照)。 -your.html):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

デモ

package forum11796699;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Value.class);

        Value value = new Value();
        value.setValue(null);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(value, System.out);
    }

}

出力

<?xml version="1.0" encoding="UTF-8"?>
<value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
于 2012-08-03T14:21:44.903 に答える