简体   繁体   English

如何使用 SimpleFramework 将对象序列化为字符串

[英]How Serialize Objects To String Using SimpleFramework

Am working with GWT application and integrated with Simple framework to parse objects into XML, I have POJO classes on client side and use the parser on server side.我正在使用 GWT 应用程序并与 Simple 框架集成以将对象解析为 XML,我在客户端有 POJO 类并在服务器端使用解析器。 I need to write the serialized object to String variable instead of file cause files not allowed in GWT App engine https://groups.google.com/forum/?fromgroups=#!topic/google-web-toolkit/M7Zo3U7CKD8 .我需要将序列化对象写入 String 变量,而不是 GWT App 引擎https://groups.google.com/forum/?fromgroups=#!topic/google-web-toolkit/M7Zo3U7CKD8 中不允许的文件原因文件。

Current code I have in the server side on GWT RPC ServiceImpl我在 GWT RPC ServiceImpl 服务器端的当前代码

File result = new File("c:/myXMLFile.xml");
Serializer serializer = new Persister();
MyBeanToSerialize beanToSerialize = new MyBeanToSerialize("firstName","LastName");
serializer.write(beanToSerialize, result);

I found the solution for returning String from the XML parser by using the writer object instead of File the code is as the following:-我找到了通过使用 writer 对象而不是 File从 XML 解析器返回 String 的解决方案,代码如下:-

String parser(){
 StringWriter writer = new StringWriter();
 Serializer serializer = new Persister();
 MyBeanToSerialize beanToSerialize = new MyBeanToSerialize("firstName","LastName");
 serializer.write(beanToSerialize, writer);
return writer.getBuffer().toString();
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM