Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to parse external URL data into AEM

Avatar

Level 3

Hi Team,

We have requirement where we need to parse external URL XML file into AEM though Json. I checked we can do with ajax call however we need to cache data temporary into AEM to process further data. Kindly provide your inputs for the same how we can achieve this in AEM.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@AK86

For hitting the servlet after page load, you can use something similar to:

 $(window).load(function() {
 // executes when complete page is fully loaded, including all frames, objects and images
 alert("window is loaded");
});

 or

 

(function(window, document, Granite, $) {
    "use strict";

    // This will trigger on page load
    $(document).on("foundation-contentloaded", function (e) {
      
    });
})(window, document, Granite, Granite.$);

Within the java class you can make the url connect and get the reponse. Code snippet:

 

URL url = new URL("http://example.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");

 

to get the xml response from the url and once you get the xml reponse, you can use the code snippet similar to https://www.javatpoint.com/convert-xml-to-json-in-java to convert the XML to JSON format.

 

Thanks

 

View solution in original post

6 Replies

Avatar

Community Advisor

Hi,

Where are you processing this? Author or Publish.

You can always store the date in the var node e.g. /var/audit/com.mysite.externaldate/data_DD_MM_YYYY

This node can be use for auditing as well.

 



Arun Patidar

Avatar

Level 3

Hi @arunpatidar - The requirement is for both environment. Whenever user load the page, the url which load in xml file format, needs to be converted into json in aem and show that parsed attributes value into a specific format on page. We also later use this data into further process where again we hit external url on click of button

Avatar

Community Advisor

Hi

For parsing of the data, it is always advised to run the Jobs (https://sling.apache.org/documentation/bundles/apache-sling-eventing-and-job-handling.html
https://ex...
) and this should be triggered when the system is at less load. i.e. during odd hours.

 

Jobs Configuration
It is recommended to not exceed 50% of the cores. To change this value, go to : http://<host>:<port>/system/console/configMgr/org.apache.sling.event.jobs.QueueConfiguration and set queue.maxparallel to a value representing 50% of the CPU cores of the server hosting your AEM instance (eg. for 8 CPU cores, set the value to 4).  

 

Thanks

Avatar

Level 3

Thank you @Shailesh_Bassi_ . However we need this to be handled at page level whenever page is loaded. We need to use this xml data from url into aem to show all attributes on page in specific format. Using this attributes we need to process it further.

Avatar

Correct answer by
Community Advisor

@AK86

For hitting the servlet after page load, you can use something similar to:

 $(window).load(function() {
 // executes when complete page is fully loaded, including all frames, objects and images
 alert("window is loaded");
});

 or

 

(function(window, document, Granite, $) {
    "use strict";

    // This will trigger on page load
    $(document).on("foundation-contentloaded", function (e) {
      
    });
})(window, document, Granite, Granite.$);

Within the java class you can make the url connect and get the reponse. Code snippet:

 

URL url = new URL("http://example.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");

 

to get the xml response from the url and once you get the xml reponse, you can use the code snippet similar to https://www.javatpoint.com/convert-xml-to-json-in-java to convert the XML to JSON format.

 

Thanks

 

Avatar

Level 3

@Shailesh_Bassi_ - Thanks for the above solution. However we need to get this data through sling model and OSGi service as this data will be used later to process it further using servlet at page level. I really appreciate if you have any lead or example for the same.