Search code examples
javaxmljaxbxstream

Marshalling java object to XML in different format?


I have below xml

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

i have constructed Note.java (data object) from it on which i do some validation

Now i want to construct XML in below format from Note.java.

One way i can think of is create MyCustomNote.java(another data object). Map fields of note.java to MyCustomNote.java. Then construct xml in below format with tools like xstream or jaxb. But i am not sure its the right or some better way is there?

<MyCustomNote>
<toAddress>Tony</toAddress>
<fromName>Jani</fromName>
<heading>Reminder</heading>
<output>someOutput</output>
</MyCustomNote>

UPDATE :-

Note.java is

public Class Note {

private String to;
private String from;
private String heading;
private String body;

//getters and setters for each the above field

}

Solution

  • so with xstream will be simple:

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

    that will be in main(), in Note.java you will have to set @Field on getMethods(). also would be great to post your Note.java too

    UPDATE:

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

    the result will be export.xml

      <MyCustomNote>
        <toAddress>Tony</toAddress>
        <fromName>Jeni</fromName>
        <heading>Reminder</heading>
        <output>Don't forget me this weekend!</output>
      </MyCustomNote>