【问题标题】:Marshalling java object to XML in different format?将java对象编组为不同格式的XML?
【发布时间】:2014-04-08 18:22:11
【问题描述】:

我有下面的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

}

【问题讨论】:

标签: java xml jaxb xstream


【解决方案1】:

所以使用 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>

【讨论】:

  • @Iulian Alexandru Costeiu。在我的更新部分下发布了 Note.java。我不确定“在 Note.java 中你必须在 getMethods() 上设置 @Field”是什么意思。能举个小例子吗?
  • 尝试不使用@Field 注释,它应该可以运行
猜你喜欢
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 2016-05-06
相关资源
最近更新 更多