Hi,
In AEM, how to read a xml file from file system and store the content as jcr:data into sling:folder node using Java?
Any sample or tutorial available, kindly help.
Solved! Go to Solution.
Views
Replies
Total Likes
Here is the HELPX article that was created based on this thread -- Scott's Digital Community: Creating a Custom Watched Folder Service for Adobe Experience Manager 6.3
Views
Replies
Total Likes
This would be done via a custom service. You can read a file on the desktop using File API. THen you can use the JCR API to create a node under a folder - which is just a JCR location- and then write the content to the node prop using JCR API logic.
To learn how to use JCR API to write data to a node prop - see this -- Scott's Digital Community: Programmatically Accessing Adobe CQ Content using the JCR API
Views
Replies
Total Likes
I will write sample JCR Java code that shows this. I will post back.
Views
Replies
Total Likes
Here is sample JCR Node API code that writes a file to the AEM JCR:
//Save the uploaded file into the specified client lib path
private
String writeToClientLib(InputStream is, String fileName, String path, String mimetype)
{
try
{
//Invoke the adaptTo method to create a Session
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(
null
);
session = resourceResolver.adaptTo(Session.
class
);
Node node = session.getNode(path);
//Get the client lib node in which to write the posted file
javax.jcr.ValueFactory valueFactory = session.getValueFactory();
javax.jcr.Binary contentValue = valueFactory.createBinary(is);
Node fileNode = node.addNode(fileName,
"nt:file"
);
fileNode.addMixin(
"mix:referenceable"
);
Node resNode = fileNode.addNode(
"jcr:content"
,
"nt:resource"
);
resNode.setProperty(
"jcr:mimeType"
, mimetype);
resNode.setProperty(
"jcr:data"
, contentValue);
Calendar lastModified = Calendar.getInstance();
lastModified.setTimeInMillis(lastModified.getTimeInMillis());
resNode.setProperty(
"jcr:lastModified"
, lastModified);
session.save();
session.logout();
// Return the path to the document that was stored in CRX.
return
fileNode.getPath();
}
Views
Replies
Total Likes
This works - just programmatically uploaded XML.
Views
Replies
Total Likes
Hi , Thanks a lot smacdonald2008. So kind of you.!!
Views
Replies
Total Likes
hi smacdonald2008,
One more thing, how to schedule the below code as job say every 5 minutes in AEM?
Views
Replies
Total Likes
Like any use case where you want to fire off in given time increments - you can write a scheduler service using AEM APIs. We have an older HELPX article that will point you in the right direction -- Adobe Experience Manager Help | Scheduling Adobe Experience Manager Jobs using Apache Sling
Views
Replies
Total Likes
Hi smacdonald2008
Could you please share the complete code?
Views
Replies
Total Likes
I will add this use case to the AEM HELPX Article list!
Views
Replies
Total Likes
This is a neat concept - kind of a concept of building a local Watched Folder. You would drop an XML file into the watched folder and AEM will pick it up even so often - based on the scheduler.
Views
Replies
Total Likes
We will have a HELPX artilce that shows this end to end in a few days!
Views
Replies
Total Likes
My AEM Scheduler Service is grabbing the XML file from the Watched Folder every min and putting it into the JCR Repo--
Views
Replies
Total Likes
Thanks smacdonald2008 .
Is this based on AEM workbench process?
Also we need to store the xml file in sling:folder (not nt:folder) as jcr:data (binary), as this is the expected format by the prefill osgi service that we have deployed already. This xml will be read in adaptive form through prefill service
Views
Replies
Total Likes
Its an AEM OSGi service written in Java.. Then anothet AEM scheduler service invokes the custom service that reads the XML file and places it within the JCR.
Views
Replies
Total Likes
Here is the HELPX article that was created based on this thread -- Scott's Digital Community: Creating a Custom Watched Folder Service for Adobe Experience Manager 6.3
Views
Replies
Total Likes
Thanks smacdonald2008.
Looking forward to this on Monday.
Views
Replies
Total Likes
We are wrapping up this today - it works very nicely as a custom AEM Service with an AEM Scheduler Service -- all discussed in the new article.
Views
Replies
Total Likes
The article is still not available. kindly let know when it will it be available
Views
Replies
Total Likes
The article is live and the URL is posted.
Views
Replies
Total Likes
Thanks smacdonald2008 i tried to install the bundle. Getting below error, any idea what could be wrong?
Views
Replies
Total Likes
Views
Likes
Replies