How to parse external URL data into AEM | Community
Skip to main content
Level 2
June 20, 2022
Solved

How to parse external URL data into AEM

  • June 20, 2022
  • 2 replies
  • 1946 views

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.

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 ShaileshBassi

@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

 

2 replies

arunpatidar
Community Advisor
Community Advisor
June 20, 2022

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
AK86Author
Level 2
June 20, 2022

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

ShaileshBassi
Community Advisor
Community Advisor
June 20, 2022

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://experienceleague.adobe.com/docs/experience-manager-64/developing/platform/dev-offloading.html?lang=en, 
) 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

AK86Author
Level 2
June 20, 2022

Thank you @shaileshbassi . 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.

ShaileshBassi
Community Advisor
ShaileshBassiCommunity AdvisorAccepted solution
Community Advisor
June 20, 2022

@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