code: public String toXml(Object root, String encoding) {
          try {
              StringWriter writer = new StringWriter();
              createMarshaller(encoding).marshal(root, writer);
              return writer.toString();
          } catch (JAXBException e) {
              throw new RuntimeException(e);
          }
      }
docs: Java Object->Xml.