gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:10 24 4
gpt4 key购买 nike

我有以下 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

}

最佳答案

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

关于java - 将java对象编码为不同格式的XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22945209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com