代码之家  ›  专栏  ›  技术社区  ›  Thufir

使用JAXB指定根节点和子节点

  •  0
  • Thufir  · 技术社区  · 6 年前

    呆在里面 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 节点到 笔记 如果我用的是正确的术语。

    这个 我的便笺本 班级:

    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 班级:

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

    exercising 这个 我的便笺本 我的记事本 类别:

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

    我期待着 grab xml 通过网络;首先需要 match 结构到 例子 .

    1 回复  |  直到 6 年前
        1
  •  3
  •   martidis    6 年前

    你很亲近。您需要更改在myNotes类中为myNotes命名xmlElement的方式。另外,mynote本身不应该有note字段(根据所需的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();
        }
    
    }