首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以不同的格式将java对象编组成XML?

以不同的格式将java对象编组成XML?
EN

Stack Overflow用户
提问于 2014-04-08 18:22:11
回答 1查看 168关注 0票数 1

我有下面的xml

代码语言:javascript
复制
<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。但我不确定这是正确的,还是有更好的办法?

代码语言:javascript
复制
<MyCustomNote>
<toAddress>Tony</toAddress>
<fromName>Jani</fromName>
<heading>Reminder</heading>
<output>someOutput</output>
</MyCustomNote>

更新:-

Note.java是

代码语言:javascript
复制
public Class Note {

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

//getters and setters for each the above field

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-11 07:01:28

因此,使用xstream将非常简单:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
 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

代码语言:javascript
复制
  <MyCustomNote>
    <toAddress>Tony</toAddress>
    <fromName>Jeni</fromName>
    <heading>Reminder</heading>
    <output>Don't forget me this weekend!</output>
  </MyCustomNote>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22945209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档