- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
抱歉,这是一个非常新的 Java 问题!
如何将一个字符串输入到 XMLEncoder 并从 XMLDecoder 输出一个字符串?
字符串包含有关 JavaBeans 对象的信息。
最佳答案
这是一个比其他问题更直接的使用 ByteArrayInput/OutputStream 的例子:
上课
static public class MyClass implements Serializable {
private String prop;
/**
* Get the value of prop
*
* @return the value of prop
*/
public String getProp() {
return prop;
}
/**
* Set the value of prop
*
* @param prop new value of prop
*/
public void setProp(String prop) {
this.prop = prop;
}
}
读或写:
static String toString(MyClass obj) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLEncoder e = new XMLEncoder(baos);
e.writeObject(obj);
e.close();
return new String(baos.toByteArray());
}
static MyClass fromString(String str) {
XMLDecoder d = new XMLDecoder(new ByteArrayInputStream(str.getBytes()));
MyClass obj = (MyClass) d.readObject();
d.close();
return obj;
}
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.setProp("propval");
String s = toString(obj);
System.out.println("s = " + s);
MyClass obj2 = fromString(s);
System.out.println("obj2.getProp() = " + obj2.getProp());
}
关于java - 如何将 XMLEncoder 和 XMLDecoder 与 String 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33970909/
我的应用程序是客户端服务器应用程序。 A 类类型的对象在客户端使用 java.beans.XMLEncoder 进行编码并保存在数据库中。 A类位于客户端。我需要在服务器端解码该对象。当我尝试使用 j
我有一个使用插件的小程序。我正在使用Java内置的X MLDecoder/Encoder系统保存该程序的数据文件。我遇到的问题是,当我使用 XMLDecoder 加载这些文件时,我收到一条消息,指出
这是在生产现场向我报告的。我们有一个消息数据库表,其中的对象使用标准 Java XMLEncoder 以 XML 形式存储。当我们处理消息时,我们从数据库中的字段中解码对象。 有时客户端会记录此错误。
使用 XMLEncoder 将 Java Bean 序列化为 XML和 XMLDecoder似乎是一种非常巧妙的方法:来自许多来源的许多类都可以可靠地序列化,只需使用它们的公共(public)接口(i
我正在尝试使用 XMLDecoder 从 XML 文件中读取对象。构造似乎没问题,但是当我调用 readObject() 方法时,我得到的是 null 而不是我期望的对象。 文件存在并且创建的 Buf
Java 有一个 transient 关键字,它与默认序列化一起使用以指示不应序列化的值。但是,如果我将 XML 序列化与 XMLDecoder 结合使用,则与该字段关联的属性仍会被序列化。我在 JS
.NET 中是否有编码 和解码 XML 的方法?我似乎找不到它们,并且想知道为什么它们不存在以及应该使用什么来代替? 我需要对 XML 文档进行编码并将其传递给 Web 服务上的字符串参数。然后需要在
出现此错误 java.lang.IllegalArgumentException: Unsupported element: net 来自此示例 xml 文件
抱歉,这是一个非常新的 Java 问题! 如何将一个字符串输入到 XMLEncoder 并从 XMLDecoder 输出一个字符串? 字符串包含有关 JavaBeans 对象的信息。 最佳答案 这是一
我正在编写一个应用程序,以下列格式读取大量基本用户详细信息;阅读后允许用户使用他们的电子邮件搜索用户的详细信息: NAME ROLE EMAIL ------
我在仅在 Docker 容器中运行的应用程序中使用 AkkaStream 中的 XmlDecoder 时遇到了问题。 错误描述 java.lang.ClassNotFoundException: co
有人可以解释为什么 FLEX 4.5 XMLDecoder 对我的 XML 数据这样做吗? var decoder:XMLDecoder = new XMLDecoder; var $object:O
我是一名优秀的程序员,十分优秀!