没有属性的JAXB元素

时间:2013-12-02 17:39:58

标签: xml jaxb

我正在使用JAXB,需要生成如下的XML代码:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!--Created with JFLAP 6.4.-->
<structure>
    <type>fa</type>
    <automaton>
        <!--The list of states.-->
        <state id="0" name="q0">
            <x>160.0</x>
            <y>151.0</y
            <initial/>                 <!-- This what I want-->
        </state>
        <state id="1" name="q1">
            <x>369.0</x>
            <y>94.0</y>
            <final/>                   <!-- This what I want-->
        </state>
        <!--The list of transitions.-->
        <transition>
            <from>0</from>
            <to>1</to>
            <read>a</read>
        </transition>
    </automaton>
</structure>

正如您所看到的,我想知道如何创建一个没有@XmlAttribute的简单@XmlElement,但在我的代码中,我得到了:

private boolean initial = false;
private boolean final   = false;

@XmlElement(name="initial")
public void setInitial(boolean val) {
    this.initial = val;
}

@XmlElement(name="final")
public void setFinal(boolean val) {
    this.final = val;
}

这样,我得到了这样的XML:

<state id="0" name="q0">
        <x>0.0</x>
        <y>0.0</y>
        <final>false</final>
        <initial>true</initial>
</state>
<state id="1" name="q1">
        <x>0.0</x>
        <y>0.0</y>
        <final>true</final>
        <initial>false</initial>
</state>

有谁知道怎么做?

2 个答案:

答案 0 :(得分:0)

您可以通过设置空字符串伪造空节点,为此您需要将boolean转为String s。

@XmlRootElement
public static  class Structure {
    @XmlElement String type;
    @XmlElement Automaton automaton;
    public Structure() { automaton = new Automaton(); }
    Structure(String t) { this(); type = t; }
}

public static class Automaton {
    @XmlElement List<State> state;
    public Automaton() { state = new ArrayList<>(); }
    State addState(State s) {state.add(s); return s;};
}

public static class State {
    @XmlAttribute String id, name;
    @XmlElement double x, y;
    @XmlElement String initial;
    @XmlElement(name="final") String final_;
    public State() {};
    State(String id, String n, double x, double y)
    {this.id = id; name = n; this.x = x; this.y = y;};
}

创建一些数据并编组:

@Test
public void jaxbEmptyEmlements() throws JAXBException {
    JAXBContext c = JAXBContext.newInstance(Structure.class);
    Marshaller m = c.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    Structure o = new Structure("fa");
    o.automaton.addState(new State("0", "q0", 160d, 151d)).initial = "";
    o.automaton.addState(new State("1", "q1", 369d, 94d)).final_ = "";
    m.marshal(o, System.out);
}

这将输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<structure>
    <type>fa</type>
    <automaton>
        <state id="0" name="q0">
            <x>160.0</x>
            <y>151.0</y>
            <initial></initial>
        </state>
        <state id="1" name="q1">
            <x>369.0</x>
            <y>94.0</y>
            <final></final>
        </state>
    </automaton>
</structure>

请注意,<initial></initial><initial/>相同,final也相同。

或者,您可以重新考虑状态类型的节点并创建元素<state-type>并使用值enum设置它,例如enum StateType { INITIAL, FINAL}或者只需将其设置为字符串{{ 1}}或initial

修改 enter image description here

答案 1 :(得分:0)

您可以执行以下操作并利用XmlAdapterBoolean转换为空对象以获取所需的XML表示。

Java模型

<强>根

该属性为boolean,但我们会设置字段Boolean,以便我们可以应用XmlAdapter。我们还将指定我们希望JAXB映射到该字段(请参阅:http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html)。

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlJavaTypeAdapter(BooleanAdapter.class)
    private Boolean foo = false;

    @XmlJavaTypeAdapter(BooleanAdapter.class)
    private Boolean bar = false;

    public boolean isFoo() {
        return foo;
    }

    public void setFoo(boolean foo) {
        this.foo = foo;
    }

    public boolean isBar() {
        return bar;
    }

    public void setBar(boolean bar) {
        this.bar = bar;
    }

}

<强> BooleanAdapter

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class BooleanAdapter extends XmlAdapter<BooleanAdapter.AdaptedBoolean, Boolean> {

    public static class AdaptedBoolean {
    }

    @Override
    public Boolean unmarshal(AdaptedBoolean v) throws Exception {
        return null != v;
    }

    @Override
    public AdaptedBoolean marshal(Boolean v) throws Exception {
        if(v) {
            return new AdaptedBoolean();
        } else {
            return null;
        }
    }

}

演示代码

下面是一些可以运行的演示代码,以确保一切正常。

<强>演示

import javax.xml.bind.*;

public class Demo {

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

        Root foo = new Root();
        foo.setFoo(true);
        foo.setBar(false);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

<强>输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <foo/>
</root>