Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Read xml file and convert it to aem page

Avatar

Level 4

Hi all,

 

I have a requirement where I need to read xml file and convert it to aem page. Is there a way to do it?

 

Regards,

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
22 Replies

Avatar

Community Advisor

Hi @rk39193348 ,

 

First we need to write OSGI service to read data and persist key value properties in CRX repo using JCR API's and once the XML data is available in JCR node properties, we can access and display it on any page.

 

Regards,

Santosh

 

 

Avatar

Level 4
Thanks for your reply. I want to convert xml to page, how can that be achieved?

Avatar

Community Advisor

@rk39193348 

There are two options available.

 

1. Import XML data by scheduler, store it in UGC & use that data on the page based on your requirement.

2. Say storing it in UGC not required, then you can skip saving it in UGC & create page whenever scheduler runs using the xml data.

 

refer this blog to import & store xml data in UGC.

https://aem.redquark.org/2019/05/import-xml-data-into-aem.html

 

Avatar

Level 4
Thanks for your reply. The article mentioned by you stores xml in jcr, which I have achieved, how can I convert it to html page?

Avatar

Community Advisor

Hi @rk39193348 

Can you add screenshot/code snippet where you are getting service resource resolver null, it will be helpful to find out the issue.

Avatar

Level 4

 @reference
private ResourceResolverFactory resourceResolverFactory;

private static final String SERVICE_USER = "service-user"; //create new service user with required permission
private static final Map<String, Object> SERVICE_USER_DETAILS = ImmutableMap
.of(ResourceResolverFactory.SUBSERVICE, (Object) SERVICE_USER);

public String createPage(String pageName) {
log.info("inside createpage****");
try {
ResourceResolver resourceResolver =resourceResolverFactory.getServiceResourceResolver(SERVICE_USER_DETAILS);
//ResourceResolver resourceResolver = slingrequest.getResourceResolver();
log.info("resourceResolver****"+resourceResolver.toString());
}

catch (Exception ex) {
log.info("AEMWatchFolderImpl error$$" + ex.getMessage());
}
return pageName;
}

using resourceResolver I am trying to create a page, but I am getting null. Can you please help?

pageManager = resourceResolver.adaptTo(PageManager.class);

newPage = pageManager.create(pagePath, pageName, templatePath, pageTitle);

Avatar

Community Advisor

@rk39193348 

Can you please highlight the line in above code where you are getting null.

Avatar

Level 4

I did this - newPage = getPageManager().create(pagePath, "ABC", templatePath, pageTitle); without using resourceResolver it worked.

Now how do I add HTML into it, programmatically?

 

Avatar

Community Advisor

@rk39193348 

Assuming as per you comment you are getting null here getPageManager().create(). once we get service resource resolver possibility of getting null return value from this method is less because this method returns page if provide parameters are valid & in some cases throws below error. so please cross check after this method execution whether page is created below this pagePath/rootPath.

 

Path not found exception : when provide pagePath not exist. (/content/site/root-page)

Access denied : when service user does not have create permission at this pagePath/rootPath. 

 

newPage = getPageManager().create(pagePath, "ABC", templatePath, pageTitle); 

Avatar

Level 4
Now how do I add html dynamically and programmatically to the page?

Avatar

Community Advisor

@rk39193348,

Here's how I would do it.

  1. Upload the XML into AEM. The XML should either be a section of your website (including with all it's children)
  2. Create an OSGI service or servlet, Java Backend, to execute the migration logic. (somehow referencing to the XML file)
  3. Implement the migration logic with the Uber Jar AEM libraries such as the PageManager API or the JCR API to clone/create page and edit pages... of course you will need to parse the XML.

 

Avatar

Correct answer by
Community Advisor

Avatar

Level 4
Thanks for your reply. The article mentioned by you stores xml in JCR, which I have achieved, how can I convert it to html page?

Avatar

Level 4
Thanks for your reply. The link mentioned by you stores xml into JCR that i have achieved. How to Convert it to HTML?

Avatar

Community Advisor

You can read the values stored in the jcr and using pageManager API, we can create page and add values in as required.

 

First it will be helpful if you come up with the mapping of fields and where/how it should stored under the page. Some of them will go to jcr:content and some go to a component. 

 

Once the mapping is done and you have required template and components created, programmatically we can add components under jcr:content and the values from xml file.  

 

Refer : https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-can-i-create-new-cq-pa... for creating pagesprogrammatically

Avatar

Level 4
Thanks. I have gone through the link provided, this line is giving depricated resolver = resolverFactory.getAdministrativeResourceResolver(null);. How do I replace this line?

Avatar

Community Advisor

Hi @rk39193348 

Check below code snippet to get service resource resolver & create service user with required permission for the same.

 

@Reference
private ResourceResolverFactory resourceResolverFactory;

private static final String SERVICE_USER = "service-user"; //create new service user with required permission
private static final Map<String, Object> SERVICE_USER_DETAILS = ImmutableMap
.of(ResourceResolverFactory.SUBSERVICE, (Object) SERVICE_USER);

private static final Logger LOGGER = LoggerFactory.getLogger(ReplicationServlet.class);

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {

if(resourceResolverFactory!= null) {
try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(SERVICE_USER_DETAILS)) {
if (resourceResolver != null) {

}
} catch (LoginException e) {
LOGGER.error("LoginException occurred", e);
}
}
}

 

Avatar

Level 4
Thanks for your reply. I am still getting null using the above code.