用JAXB指定根和子节点
停留在JAXB
我将如何重构MyNote
以使其符合:
<note> <to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
这是很好但不是有效的,我的理解。电流输出:
<?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>
太平坦了,而不是像例子一样嵌套。
我相信,如果我使用的是正确的术语,这将构成note
该root
元素,其他元素将成为该元素的children
节点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
类:
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
类:
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);
}
我期待抓住这个xml
通过网络;
首先需要将结构与示例匹配。
回答:
你很亲密
您需要更改在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();
}
}
以上是 用JAXB指定根和子节点 的全部内容, 来源链接: utcz.com/qa/399490.html