TO learn how to persist data in the AEM JCR -- see this AEM article:
http://helpx.adobe.com/experience-manager/using/persisting-cq-data-java-content1.html
You use the JCR API to store data - as shown in this article. You can place that logic into an OSGi bundle.
To learn how to query data from the AEM JCR - see this article:
http://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html
TO learn how to write a custom workflow step - see this community article:
http://helpx.adobe.com/experience-manager/using/creating-custom-aem-workflow-steps.html
That custom workflow step creates an workflow step that sends email - but once you understand how to create a custom step - you can change the application logic to meet your business requirements.
You can also use W3C Java APIS to read and write to XML. SO you can use this logic in custom steps. I believe there is an example of dynamically creating XML in the Query article listed above.