Read a xml file from file system and store as jcr:data in crx | Community
Skip to main content
Level 3
Feb 26, 2018 5:17 PM UTC
Solved

Read a xml file from file system and store as jcr:data in crx

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
smacdonald2008
smacdonald2008Accepted solution
Level 10
Mar 1, 2018 7:20 PM UTC

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

22 replies

smacdonald2008
Level 10
Mar 1, 2018 12:51 AM UTC

My AEM Scheduler Service is grabbing the XML file from the Watched Folder every min and putting it into the JCR Repo--

Translate
smacdonald2008
Level 10
Mar 1, 2018 12:28 AM UTC

We will have a HELPX artilce that shows this end to end in a few days!

Translate
smacdonald2008
Level 10
Feb 28, 2018 7:45 PM UTC

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.

Translate
smacdonald2008
Level 10
Feb 28, 2018 7:30 PM UTC

I will add this use case to the AEM HELPX Article list!

Translate
Level 3
Feb 28, 2018 6:42 PM UTC

Hi smacdonald2008​

Could you please share the complete code?

Translate
smacdonald2008
Level 10
Feb 27, 2018 5:09 PM UTC

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

Translate
Level 3
Feb 27, 2018 4:54 PM UTC

hi smacdonald2008​,

One more thing, how to schedule the below code as job say every 5 minutes in AEM?

Translate
Level 3
Feb 27, 2018 2:08 AM UTC

Hi , Thanks a lot smacdonald2008​. So kind of you.!!

Translate
smacdonald2008
Level 10
Feb 26, 2018 7:10 PM UTC

This works - just programmatically uploaded XML.

Translate
smacdonald2008
Level 10
Feb 26, 2018 7:06 PM UTC

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();

}

Translate