使用JAXB

时间:2019-01-08 02:21:07

标签: java xml jaxb marshalling well-formed

停留在JAXB内,我将如何重构MyNote使其conforms达到:

<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

据我所知,is格式正确,但无效。当前输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyNotes>
    <Note>
        <note>XY3Z1RGEO9W79LALCS</note>
        <to>LJAY9RNMUGGENGNND9</to>
        <from>GOVSHVZ3GJWC864L7X</from>
        <heading>EX6LGVE5LGY4A6B9SK</heading>
        <body>L95WYQNMEU1MFDRBG4</body>
    </Note>
</MyNotes>

太平坦,而不是嵌套为example

我相信这会使note成为root元素,如果我使用正确的术语,则其他元素是children的{​​{1}}节点。

note类:

MyNote

package net.bounceme.dur.jaxb.hello.world; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlType(propOrder = {"note", "to", "from", "heading", "body"}) @XmlRootElement(name = "note") public class MyNote { private String note; private String to; private String from; private String heading; private String body; public String getNote() { return note; } @XmlElement(name = "note") public void setNote(String note) { this.note = note; } public String getTo() { return to; } @XmlElement(name = "to") public void setTo(String to) { this.to = to; } public String getFrom() { return from; } @XmlElement(name = "from") public void setFrom(String from) { this.from = from; } public String getHeading() { return heading; } @XmlElement(name = "heading") public void setHeading(String heading) { this.heading = heading; } public String getBody() { return body; } @XmlElement(name = "body") public void setBody(String body) { this.body = body; } @Override public String toString() { return note + to + from + heading + body; } } 类:

MyNotes

exercising package net.bounceme.dur.jaxb.hello.world; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "MyNotes") public class MyNotes { private static final Logger LOG = Logger.getLogger(MyNotes.class.getName()); private List<MyNote> myNotes = new ArrayList<>(); public MyNotes() { } public List<MyNote> getMyNotes() { LOG.info(myNotes.toString()); return myNotes; } @XmlElement(name = "Note") public void setMyNotes(List<MyNote> myNotes) { LOG.info(myNotes.toString()); this.myNotes = myNotes; } public void add(MyNote myNote) { LOG.info(myNote.toString()); myNotes.add(myNote); } @Override public String toString() { StringBuffer str = new StringBuffer(); for (MyNote note : this.myNotes) { str.append(note.toString()); } return str.toString(); } } MyNote类:

MyNotes

我正在寻找grab public MyNotes unmarshallMyNotesFromFile(URI uri) throws Exception { File file = new File(uri); JAXBContext jaxbContext = JAXBContext.newInstance(MyNotes.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); MyNotes myNotes = (MyNotes) jaxbUnmarshaller.unmarshal(file); return myNotes; } public void marshallMyNotesAndWriteToFile(MyNotes notes, URI uri) throws Exception { JAXBContext jaxbContext = JAXBContext.newInstance(MyNotes.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(notes, new File(uri)); jaxbMarshaller.marshal(notes, System.out); } 通过网络;首先需要将match的结构设置为example

1 个答案:

答案 0 :(得分:3)

您非常亲密。您需要更改在MyNotes类中为myNotes命名xmlElement的方式。此外,MyNote本身不应该具有注释字段(根据您所需的xml)。您编辑的类如下所示(为方便起见,我也删除了日志记录语句):

@XmlType(propOrder = { "to", "from", "heading", "body"})
@XmlRootElement(name = "note")
public class MyNote {

    private String to;
    private String from;
    private String heading;
    private String body;

    public String getTo() {
        return to;
    }

    @XmlElement(name = "to")
    public void setTo(String to) {
        this.to = to;
    }

    public String getFrom() {
        return from;
    }

    @XmlElement(name = "from")
    public void setFrom(String from) {
        this.from = from;
    }

    public String getHeading() {
        return heading;
    }

    @XmlElement(name = "heading")
    public void setHeading(String heading) {
        this.heading = heading;
    }

    public String getBody() {
        return body;
    }

    @XmlElement(name = "body")
    public void setBody(String body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return  to + from + heading + body;
    }

}

和MyNotes:

@XmlRootElement(name = "MyNotes")
public class MyNotes {

    private List<MyNote> myNotes = new ArrayList<>();

    public MyNotes() {
    }

    public List<MyNote> getMyNotes() {
        return myNotes;
    }

    @XmlElement(name = "note")
    public void setMyNotes(List<MyNote> myNotes) {
        this.myNotes = myNotes;
    }

    public void add(MyNote myNote) {
        myNotes.add(myNote);
    }

    @Override
    public String toString() {
        StringBuffer str = new StringBuffer();
        for (MyNote note : this.myNotes) {
            str.append(note.toString());
        }
        return str.toString();
    }

}