jersey map to xml

前端之家收集整理的这篇文章主要介绍了jersey map to xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//@source:http://stackoverflow.com/questions/11353790/serializer-for-hashmaps-for-jersey-use
importjava.util.*;
importjavax.xml.bind.annotation.adapters.XmlAdapter;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;

publicclassMapAdapterextendsXmlAdapter<Element,Map<String,String>>{

privateDocumentBuilderdocumentBuilder;

publicMapAdapter()throwsException{
documentBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
}

@Override
publicElementmarshal(Map<String,String>map)throwsException{
Documentdocument=documentBuilder.newDocument();
ElementrootElement=document.createElement("data");
document.appendChild(rootElement);
for(Map.Entry<String,String>entry:map.entrySet()){
ElementchildElement=document.createElement(entry.getKey());
childElement.setTextContent(entry.getValue());
rootElement.appendChild(childElement);
}
returnrootElement;
}

@Override
publicMap<String,String>unmarshal(ElementrootElement)throwsException{
NodeListnodeList=rootElement.getChildNodes();
Map<String,String>map=newHashMap<String,String>(nodeList.getLength());
for(intx=0;x<nodeList.getLength();x++){
Nodenode=nodeList.item(x);
if(node.getNodeType()==Node.ELEMENT_NODE){
map.put(node.getNodeName(),node.getTextContent());
}
}
returnmap;
}

}
@XmlAccessorType(XmlAccessType.FIELD)
	ClassName{
@XmlAnyElement
@XmlJavaTypeAdapter(MapAdapter.class)
	privateMapargs;
	}
原文链接:https://www.f2er.com/xml/299389.html

猜你在找的XML相关文章