Expand my Community achievements bar.

SOLVED

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

Avatar

Level 3

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.

1 Accepted Solution

Avatar

Correct answer by
Level 10
22 Replies

Avatar

Level 10

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

Avatar

Level 10

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

Avatar

Level 10

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

}

Avatar

Level 10

This works - just programmatically uploaded XML.

XML.png

Avatar

Level 3

hi smacdonald2008​,

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

Avatar

Level 10

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

Avatar

Level 3

Hi smacdonald2008

Could you please share the complete code?

Avatar

Level 10

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

Avatar

Level 10

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.

Avatar

Level 10

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

Avatar

Level 10

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

Files.png

Avatar

Level 3

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

Avatar

Level 10

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.

Avatar

Correct answer by
Level 10

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

Avatar

Level 3

Thanks smacdonald2008​.

Looking forward to this on Monday.

Avatar

Level 10

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.

Avatar

Level 3

hi smacdonald2008

The article is still not available. kindly let know when it will it be available

Avatar

Level 10

The article is live and the URL is posted.

Avatar

Level 3

Thanks smacdonald2008 i tried to install the bundle. Getting below error, any idea what could be wrong?