jersey map to xml
发布时间:2020-12-16 05:10:51 所属栏目:百科 来源:网络整理
导读://@source:http://stackoverflow.com/questions/11353790/serializer-for-hashmaps-for-jersey-useimportjava.util.*;importjavax.xml.bind.annotation.adapters.XmlAdapter;importjavax.xml.parsers.*;importorg.w3c.dom.*;publicclassMapAdapterextendsXml
//@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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |