- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用Xstream序列化Java中的对象列表,我的核心代码是:
//register alias
xstream.alias("category", ServerCategory.class);
//register converter
xstream.registerConverter(new ServerCategoryConverter());
// get all categories
List<ServerCategory> categoryList = categoryDao.getAllEager();
// serialize all categories
for(ServerCategory category : categoryList) {
xstream.toXML(category, streamWriter);
}
这工作得很好,但我在 </category>
之后没有换行符-tag 这似乎与 xml 规范冲突,导致每个读者都会在这一行中断。有解决办法吗?
最佳答案
您当前的代码将生成如下所示的内容:
<category>
<id>1</id>
<name>One</name>
<description>One</description>
<syncversion>V1</syncversion>
<parents/>
</category>
<category>
<id>2</id>
<name>Two</name>
<description>Two</description>
<syncversion>V1</syncversion>
<parents>
<parent>1</parent>
</parents>
</category>
这是无效的 xml,因为它应该有一个根元素(此 xml 有多个根元素类别)理想情况下你应该像这样生成。您将能够在浏览器中打开下面的内容。
<list> <!-- one root element required!!-->
<category>
<id>1</id>
<name>One</name>
<description>One</description>
<syncversion>V1</syncversion>
<parents/>
</category>
<category>
<id>2</id>
<name>Two</name>
<description>Two</description>
<syncversion>V1</syncversion>
<parents>
<parent>1</parent>
</parents>
</category>
<list>
需要做的是代替
for(ServerCategory category : categoryList) {
xstream.toXML(category, streamWriter);
}
你应该序列化整个列表
xstream.toXML(categoryList, streamWriter);
并且您的 ServerCategoryConverter canCovert() 应该被覆盖
public boolean canConvert(Class foo) {
//dont use this converter for the Arraylist. Arraylist will be handeld by the default converter
if (foo.getName().equals("java.util.ArrayList")) {
return false;
}
return true;
}
我附上了完整的代码以供引用。它适用于上述修复:
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class XSTReamTest {
/*
* ExpertService.class
*/
public static void main(String args[]) throws Exception {
XSTReamTest tst = new XSTReamTest();
tst.createXmlExport();
}
public File createXmlExport() throws Exception {
XStream xstream = new XStream();
File xmlExportFile = null;
BufferedOutputStream outputStream = null;
OutputStreamWriter streamWriter = null;
try {
xmlExportFile = new File("easylearncards1_exportRequest_.xml");
System.out.println("Path = " + xmlExportFile.getAbsolutePath());
outputStream = new BufferedOutputStream(new FileOutputStream(xmlExportFile));
streamWriter = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
exportCategories(xstream, streamWriter);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
streamWriter.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return xmlExportFile;
}
private void exportCategories(XStream xstream, OutputStreamWriter streamWriter) {
// register alias
xstream.alias("category", ServerCategory.class);
// register converter
xstream.registerConverter(new ServerCategoryConverter());
// get all categories
List<ServerCategory> categoryList = new ArrayList<ServerCategory>();
ServerCategory root = new ServerCategory("1", "One", "One", "V1", null);
ServerCategory child1 = new ServerCategory("2", "Two", "Two", "V1", new ServerCategory[]{root});
ServerCategory child2 = new ServerCategory("3", "Three", "Three", "V1", new ServerCategory[]{root});
ServerCategory child3 = new ServerCategory("4", "Four", "Four", "V1", new ServerCategory[]{child1, child2});
categoryList.add(root);
categoryList.add(child1);
categoryList.add(child2);
categoryList.add(child3);
// convert all categories
// for (ServerCategory category : categoryList) {
// xstream.toXML(category, streamWriter);
// }
xstream.toXML(categoryList, streamWriter);
}
static class ServerCategory {
String id;
String name;
String description;
String syncVersion;
ServerCategory parents[];
ServerCategory(String id, String name, String description, String syncVersion, ServerCategory[] parents) {
this.id = id;
this.name = name;
this.description = description;
this.syncVersion = syncVersion;
this.parents = parents;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSyncVersion() {
return syncVersion;
}
public void setSyncVersion(String syncVersion) {
this.syncVersion = syncVersion;
}
public ServerCategory[] getParents() {
return parents;
}
public void setParents(ServerCategory[] parents) {
this.parents = parents;
}
}
/*
* ServerCategoryConverter.class
*/
static class ServerCategoryConverter implements Converter {
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
ServerCategory category = (ServerCategory) value;
writer.startNode("id");
writer.setValue(category.getId().toString());
writer.endNode();
writer.startNode("name");
writer.setValue(category.getName());
writer.endNode();
writer.startNode("description");
writer.setValue(category.getDescription());
writer.endNode();
writer.startNode("syncversion");
writer.setValue(category.getSyncVersion().toString());
writer.endNode();
writer.startNode("parents");
if (category.getParents() != null) {
for (ServerCategory parent : category.getParents()) {
writer.startNode("parent");
writer.setValue(parent.getId().toString());
writer.endNode();
}
}
writer.endNode();
}
public java.lang.Object unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader hierarchicalStreamReader, com.thoughtworks.xstream.converters.UnmarshallingContext unmarshallingContext) {
return null;
}
public boolean canConvert(Class foo) {
if (foo.getName().equals("java.util.ArrayList")) {
return false;
}
return true;
}
}
}
关于java - XStream 使用 toXML(Object, StreamWriter) 缺少换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14633133/
StreamWriter.Flush() 和 StreamWriter.Close() 在功能上有什么区别? 当我的数据没有正确写入文件时,我将 Flush() 和 Close() 添加到我的代码末尾
我需要尽快写入许多文件,但我没有像我使用的那样使用 Streamwriter 获得我想要的结果。例如,如果我写... $File = "c:\somefile" $Contents = get-con
我正在尝试创建一个程序,将文本文件中的几行从文本框编辑为用户设置值。 至少,在我开始使用这个值设置之前。我什至无法编辑该文件。这段代码有什么问题?我实际上尝试了更多示例,但没有一个起作用。 priva
遇到一个问题,streamwriter 在我正在生成的 csv 中生成错误的字符。字符  只出现在文件的开头: 5,"GEN",555555555,,"Evan","Smith",,,,,
我正在尝试创建一个程序,从文本框中将文本文件中的几行编辑为用户设置的值。 至少,在我懒得开始这个值设置之前。我什至无法编辑文件。这段代码有什么问题?我实际上尝试了更多示例,但没有一个有效。 priva
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
如何写入不同类的文件? public class gen { public static string id; public static string m_graph_file; } s
我正在使用 StreamWriter 类将一些文本写入新创建的文本文件,但输出总是出奇地错误,这是我的代码: StreamWriter sw = new StreamWriter("newtextfi
basicLog 是名称和时间戳的列表。我想将它们写入文件。我得到的两个错误是在 ';'在 StreamWriter 行和"file"上。在第二行。 ';'错误是:可能错误的空语句文件错误是:当前上下
这是我的代码。 : FileStream fileStreamRead = new FileStream(pathAndFileName, FileMode.OpenOrCreate, FileAcc
我已经初始化了 StreamWriter 实例并将其与“MyFile.txt”相关联 System.IO.StreamWriter sw = new StreamWriter("MyFile.txt"
我在字符串变量中有大量内容,我想使用流写入器将该内容写入文本文件。但是流编写器正在截断文本而不是将整个内容写入文件。为什么? using (StreamWriter sw = new StreamWr
我有一种方法可以将文件作为附件发送。我使用 StreamWriter 和 MemoryStream 来创建附件。代码如下: public void ComposeEmail(string from,
我试图每小时创建一个新的日志文件,并在服务器上运行以下代码。当天的第一个日志文件正在创建并写入正常,但当天没有创建更多的日志文件。任何想法可能出了什么问题?也不会抛出异常。 private void
我正在为我的游戏制作一个关卡编辑器,大部分工作正常,除了...当我尝试保存我的文件 (XML) 时,文件没有创建,在输出框中我得到: “System.NullReferenceException”类型
我在 form1 中有一个文本框和一个按钮,当我在文本框中书写时,我可以使用我的按钮将其保存到计算机上的文件中。这是放在我的按钮里面 public void button1_Click(obj
我目前的申请有问题,我开始认为这只是我的逻辑。即使浏览了这些表格和 MSDN,我也无法弄清楚。 我正在尝试使用 StreamWriter 在我的应用程序目录中创建文本文档并创建包含该文档的子文件夹。目
我组织了一个小类来实现一个快速记录器。我用一个在构造函数中实例化的私有(private) System.IO.StreamWriter 组成它。因为我使用的方式阻止我实现 using block ,所
我有一个使用此方法的 C# .NET 3.5 Windows 窗体程序: private void toLog(string sLog) { richTextBox1.
这里真的很迷茫。我正在运行 Windows 7 并使用管理员帐户,但由于某种原因,以下代码正在下降; public static readonly string Report = Path.Combi
我是一名优秀的程序员,十分优秀!