Read a xml file from file system and store as jcr:data in crx | Community
Skip to main content
Level 3
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
Level 10
February 26, 2018

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

smacdonald2008
Level 10
February 26, 2018

I will write sample JCR Java code that shows this. I will post back.

smacdonald2008
Level 10
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();

}

smacdonald2008
Level 10
February 26, 2018

This works - just programmatically uploaded XML.

Level 3
February 27, 2018

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

Level 3
February 27, 2018

hi smacdonald2008​,

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

smacdonald2008
Level 10
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

Level 3
February 28, 2018

Hi smacdonald2008

Could you please share the complete code?

smacdonald2008
Level 10
February 28, 2018

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

smacdonald2008
Level 10
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.