【问题标题】:Specifying root and child nodes with JAXB使用 JAXB 指定根节点和子节点
【发布时间】:2019-01-08 02:21:06
【问题描述】:

留在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 元素,如果我使用正确的术语,其他元素是 childrennote 的节点。

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 类:

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 MyNoteMyNotes 类:

    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的结构转为example

【问题讨论】:

    标签: java xml jaxb marshalling well-formed


    【解决方案1】:

    你很亲密。您需要更改在 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;
        }
    
    }
    

    和我的笔记:

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

    【讨论】:

    • 您确定将tofromheadingbody 作为note 的子节点缩进吗?
    • @Thufir,是的,我试过了。你有什么不同的经历吗?
    • 是的,您的解决方案有效,答案当然被接受。 MyNotenote 属性上没有 gettter/setter,因为 note 是根属性?跨度>
    • @Thufir 我完全从 MyNote 类中删除了 note 字段,因为您希望 note 元素中的 xml 只有 to、from、heading 和 body。否则,您将在便笺中添加便笺。
    • 您可以使用 XmlElementWrapper 在您的 xml 中添加一个额外的层。如文档中所述,它通常用于包装集合。例如,如果您想要(出于某种原因)...,您可以在 setMyNotes 方法上方添加 @XmlElementWrapper(name="TechNotes") .它环绕 XmlElement,因此您不能使用它同时将注释环绕到、从、标题、正文。他们需要一个共同的父母。
    猜你喜欢
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多