Esempio n. 1
0
 // 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();
 }
Esempio n. 2
0
 /* 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;
 }
Esempio n. 3
0
  /**
   * Reads an XML document from an input source and copies its values into the specified object
   *
   * @param ob The object to receive the values
   * @param source The location of the XML document
   * @throws IOException If there is an error reading the document
   */
  public void readObject(Object ob, InputSource source) throws IOException {
    try {
      // Create a document builder to read the document
      DocumentBuilder builder = factory.newDocumentBuilder();

      // Read the document
      Document doc = builder.parse(source);

      // Get the root element
      Element element = doc.getDocumentElement();

      // Copy the root element into the bean
      readObject(ob, element);
    } catch (SAXException exc) {
      throw new IOException("Error parsing XML document: " + exc.toString());
    } catch (ParserConfigurationException exc) {
      throw new IOException("Error parsing XML document: " + exc.toString());
    }
  }