/* PRIVATE METHODS */ private DOMDocument createDomDocument(String rootName) { DOMDocument domDocument = new DOMDocument(nsURI, rootName, null); Document document = domDocument.getDocument(); Element root = document.getDocumentElement(); if (includeTypeInfo) { root.setAttributeNS( XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE + ":" + nsPrefix, nsURI); root.setAttributeNS( XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE + ":" + NamespaceConstants.NSPREFIX_SCHEMA_XSD, NamespaceConstants.NSURI_SCHEMA_XSD); root.setAttributeNS( XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE + ":" + NamespaceConstants.NSPREFIX_SCHEMA_XSI, NamespaceConstants.NSURI_SCHEMA_XSI); root.setAttributeNS( XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE + ":" + NamespaceConstants.NSPREFIX_SOAP_ENCODING, NamespaceConstants.NSURI_SOAP_ENCODING); root.setAttributeNS( XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE + ":" + Constants.NS_PREFIX_XMLSOAP, Constants.NS_URI_XMLSOAP); } return domDocument; }
// Serialize the bean using the specified namespace prefix & uri public String serialize(Object bean) throws IntrospectionException, IllegalAccessException { // Use the class name as the name of the root element String className = bean.getClass().getName(); String rootElementName = null; if (bean.getClass().isAnnotationPresent(ObjectXmlAlias.class)) { AnnotatedElement annotatedElement = bean.getClass(); ObjectXmlAlias aliasAnnotation = annotatedElement.getAnnotation(ObjectXmlAlias.class); rootElementName = aliasAnnotation.value(); } // Use the package name as the namespace URI Package pkg = bean.getClass().getPackage(); nsURI = pkg.getName(); // Remove a trailing semi-colon (;) if present (i.e. if the bean is an array) className = StringUtils.deleteTrailingChar(className, ';'); StringBuffer sb = new StringBuffer(className); String objectName = sb.delete(0, sb.lastIndexOf(".") + 1).toString(); domDocument = createDomDocument(objectName); document = domDocument.getDocument(); Element root = document.getDocumentElement(); // Parse the bean elements getBeanElements(root, rootElementName, className, bean); StringBuffer xml = new StringBuffer(); if (prettyPrint) xml.append(domDocument.serialize(lineSeperator, indentChars, includeXmlProlog)); else xml.append(domDocument.serialize(includeXmlProlog)); if (!includeTypeInfo) { int index = xml.indexOf(root.getNodeName()); xml.delete(index - 1, index + root.getNodeName().length() + 2); xml.delete(xml.length() - root.getNodeName().length() - 4, xml.length()); } return xml.toString(); }
public Object deserialize(Class rootClass, String xml) throws Exception { DOMDocument domDocument = new DOMDocument(xml); root = domDocument.getRootNode(); if ((classPackage == null) || (rootPath == null)) classLoaderStrategy = getClassLoaderStrategy(rootClass.getName()); else { currentPackage = classPackage; classLoaderStrategy = ClassLoaderUtil.getClassLoader( ClassLoaderUtil.FILE_SYSTEM_CLASS_LOADER, new String[] {rootPath}); } return deserialize(root, true, true); }