我正在尝试使用JAXB将HashTable<String, String>
序列化为XML。我对Java很陌生(来自C#),所以我对这个任务感到困惑。如何使用JAXB将HashTable <String,String>序列化为XML?
我看到了下面的代码:
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
是错误的(我已经知道了)。
那里的Java大师能告诉我如何调用这段代码吗?提出一个更简单的实现(当然还有一个调用示例)也是非常受欢迎的。
谢谢。
JAXB确实有映射的默认映射(请参阅http://stackoverflow.com/questions/7534500/how-to-serialize-hashtablestring-string-to-xml-using-jaxb/7534671#7534671)。您所描述的策略对于指定非默认映射是必要的。 –