将java对象编组为不同格式的XML?

将java对象编组为不同格式的XML?,java,xml,jaxb,xstream,Java,Xml,Jaxb,Xstream,我有下面的xml <note> <to>Tony</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 因此,使用xstream将非常简单: final XStream xstream = new XStream(new Sta

我有下面的xml

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

因此,使用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)));

}
}

注.类别

 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>

以下内容可能会有所帮助:@Iulian Alexandru Costeiu。在我的更新部分下发布Note.java。我不知道你在Note.java中的意思是什么,你必须在getMethods上设置@Field。你能举个小例子吗?不带@Field注释试试,它应该可以运行
 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;
}
  <MyCustomNote>
    <toAddress>Tony</toAddress>
    <fromName>Jeni</fromName>
    <heading>Reminder</heading>
    <output>Don't forget me this weekend!</output>
  </MyCustomNote>