Read a xml file from file system and store as jcr:data in crx | Community
Skip to main content
New Participant
February 26, 2018
Solved

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

  • February 26, 2018
  • 22 replies
  • 14082 views

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.
Best answer by smacdonald2008

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
New Participant
March 1, 2018

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

smacdonald2008
New Participant
March 1, 2018

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

smacdonald2008
New Participant
February 28, 2018

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.

smacdonald2008
New Participant
February 28, 2018

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

New Participant
February 28, 2018

Hi smacdonald2008

Could you please share the complete code?

smacdonald2008
New Participant
February 27, 2018

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

New Participant
February 27, 2018

hi smacdonald2008​,

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

New Participant
February 27, 2018

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

smacdonald2008
New Participant
February 26, 2018

This works - just programmatically uploaded XML.

smacdonald2008
New Participant
February 26, 2018

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

}