我有下面的xml
<note>
<to>Tony</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>我从它构造了Note.java (数据对象),并在其上进行了一些验证。
现在,我想从Note.java构建以下格式的XML。
我可以想到的一种方法是创建MyCustomNote.java(另一个数据对象)。将note.java字段映射到MyCustomNote.java。然后使用xstream或jaxb等工具以下面的格式构造xml。但我不确定这是正确的,还是有更好的办法?
<MyCustomNote>
<toAddress>Tony</toAddress>
<fromName>Jani</fromName>
<heading>Reminder</heading>
<output>someOutput</output>
</MyCustomNote>更新:-
Note.java是
public Class Note {
private String to;
private String from;
private String heading;
private String body;
//getters and setters for each the above field
}发布于 2014-04-11 07:01:28
因此,使用xstream将非常简单:
final XStream xstream = new XStream(new StaxDriver());
xstream.alias("MyCustomNote", Note.class);
xstream.aliasField("toAddress", Note.class,"to");
xstream.aliasField("fromName", Note.class,"from");
xstream.aliasField("heading", Note.class,"heading");
xstream.aliasField("output", Note.class,"body");这将在main()中,在Note.java中,必须在getMethods()上设置@Field。同时也很高兴发布你的Note.java。
更新:
ExportToXml.class
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.StaxDriver;
public class ExportToXml {
public Note createNote() {
Note container = new Note();
container.setBody("Don't forget me this weekend!");
container.setFrom("Jeni");
container.setHeading("Reminder");
container.setTo("Tony");
return container;
}
public static void main(String[] args){
final XStream xstream = new XStream(new StaxDriver());
xstream.alias("MyCustomNote", Note.class);
xstream.aliasField("toAddress", Note.class,"to");
xstream.aliasField("fromName", Note.class,"from");
xstream.aliasField("heading", Note.class,"heading");
xstream.aliasField("output", Note.class,"body");
ExportToXml export = new ExportToXml();
Note firstNote = export.createNote();
final File file = new File("D:\\export.xml");
BufferedOutputStream stdout;
try {
stdout = new BufferedOutputStream(new FileOutputStream(file));
} catch (final FileNotFoundException e) {
throw new RuntimeException(e);
}
xstream.marshal(firstNote, new PrettyPrintWriter(
new OutputStreamWriter(stdout)));
}}
Note.class
public class Note {
private String to;
private String from;
private String heading;
private String body;
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getHeading() {
return heading;
}
public void setHeading(String heading) {
this.heading = heading;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}结果将是export.xml
<MyCustomNote>
<toAddress>Tony</toAddress>
<fromName>Jeni</fromName>
<heading>Reminder</heading>
<output>Don't forget me this weekend!</output>
</MyCustomNote>https://stackoverflow.com/questions/22945209
复制相似问题