public static String marshal( Object msg, String msgQName, String msgLocalName, String[] userPackages) throws Exception { TreeSet<String> contextPackages = new TreeSet<String>(); for (int i = 0; i < userPackages.length; i++) { String userPackage = userPackages[i]; contextPackages.add(userPackage); } JAXBContext jaxbContext = JAXBUtils.getJAXBContext( contextPackages, constructionType, contextPackages.toString(), XmlUtils.class.getClassLoader(), new HashMap<String, Object>()); Marshaller marshaller = JAXBUtils.getJAXBMarshaller(jaxbContext); JAXBElement jaxbRequest = new JAXBElement(new QName(msgQName, msgLocalName), msg.getClass(), msg); Writer writer = new StringWriter(); // Support XMLDsig XMLEventWriter xmlWriter = staxOF.createXMLEventWriter(writer); marshaller.marshal(jaxbRequest, xmlWriter); xmlWriter.flush(); JAXBUtils.releaseJAXBMarshaller(jaxbContext, marshaller); return writer.toString(); }
public static Object unmarshal(String msg, String userPackages[]) throws Exception { TreeSet<String> contextPackages = new TreeSet<String>(); for (int i = 0; i < userPackages.length; i++) { String userPackage = userPackages[i]; contextPackages.add(userPackage); } JAXBContext jaxbContext = JAXBUtils.getJAXBContext( contextPackages, constructionType, contextPackages.toString(), XmlUtils.class.getClassLoader(), new HashMap<String, Object>()); Unmarshaller unmarshaller = JAXBUtils.getJAXBUnmarshaller(jaxbContext); Object o = unmarshaller.unmarshal(staxIF.createXMLStreamReader(new StringSource(msg))); JAXBUtils.releaseJAXBUnmarshaller(jaxbContext, unmarshaller); if (o instanceof JAXBElement) return ((JAXBElement) o).getValue(); return o; }