How to serialize HashTablelt;String, Stringgt; to XML using JAXB?(如何序列化HashTablelt;String,Stringgt;使用 JAXB 到 XML?)
问题描述
我正在尝试使用 JAXB 将 HashTable<String, String>
序列化为 XML.我对 Java 很陌生(来自 C#),所以我对这个任务有点困惑.
I am trying to use JAXB to serialize a HashTable<String, String>
to XML. I am very new to Java (came from C#), so I am kinda perplexed by this task.
我看过以下代码:
public static <T> String ObjectToXml(T object, Class<T> classType) throws JAXBException
{
JAXBContext jaxbContext = JAXBContext.newInstance(classType);
StringWriter writerTo = new StringWriter();
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(object, writerTo); //create xml string from the input object
return writerTo.toString();
}
像这样调用:ObjectToXml(o, ClassOfO.class)
,但是 HashTable<String, String>.class
是错误的(我已经知道了).
Which is invoked like so: ObjectToXml(o, ClassOfO.class)
, but HashTable<String, String>.class
is wrong (that I already know).
那里的 Java 专家可以告诉我如何调用此代码吗?也欢迎提出更简单的实现(当然还有调用示例).
Can Java gurus out there show me how to invoke this code? Proposing a simpler implementation (along with an invocation example, of course) is most welcome as well.
谢谢.
推荐答案
您需要创建一个包装类来保存 Hashtable
:
You will need to create a wrapper class to hold onto the Hashtable
:
package forum7534500;
import java.util.Hashtable;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Wrapper {
private Hashtable<String, String> hashtable;
public Hashtable<String, String> getHashtable() {
return hashtable;
}
public void setHashtable(Hashtable<String, String> hashtable) {
this.hashtable = hashtable;
}
}
然后您可以执行以下操作:
Then you can do the following:
package forum7534500;
import java.io.StringWriter;
import java.util.Hashtable;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Wrapper.class);
Wrapper wrapper = new Wrapper();
Hashtable<String, String> hashtable = new Hashtable<String,String>();
hashtable.put("foo", "A");
hashtable.put("bar", "B");
wrapper.setHashtable(hashtable);
System.out.println(objectToXml(jc, wrapper));
}
public static String objectToXml(JAXBContext jaxbContext, Object object) throws JAXBException
{
StringWriter writerTo = new StringWriter();
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(object, writerTo); //create xml string from the input object
return writerTo.toString();
}
}
这将产生以下输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<wrapper>
<hashtable>
<entry>
<key>bar</key>
<value>B</value>
</entry>
<entry>
<key>foo</key>
<value>A</value>
</entry>
</hashtable>
</wrapper>
注意事项
JAXBContext
是一个线程安全的对象,应该被创建一次并重复使用.Hashtable
是同步的,如果你不需要这个,那么使用HashMap
是常见的替换.- 惯例是以小写字母开头的 Java 方法名称.
JAXBContext
is a thread-safe object and should be created once and reused.Hashtable
is synchronized, if you do not need this then usingHashMap
is the common replacement.- The convention is to start Java method names with a lower case letter.
自定义映射
您可以在 JAXB 中使用 XmlAdapter
来自定义任何类的映射.下面是我博客上一篇文章的链接,我在其中演示了如何做到这一点:
You can use an XmlAdapter
in JAXB to customize the mapping of any class. Below is an link to a post on my blog where I demonstrate how to do just that:
- http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
这篇关于如何序列化HashTable<String,String>使用 JAXB 到 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何序列化HashTable<String,String>使用 JAXB 到 XML?
基础教程推荐
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01