Forum Discussion

SheryBulos's avatar
5 years ago

How to read XML file content?

Hello,
Please I want to read an XML file content which exists in the assets in studio?

<#assign doc= ${asset.get('/html/assets/companyProducts.xml')}/>
${doc}


my intention is to pass this doc to macro to view each product.
so how can I achieve this? because for doc is empty I cant view the products 

PS. I also tried this one and its not working 

Map root = new HashMap();
root.put(
"doc",
freemarker.ext.dom.NodeModel.parse(new File(${asset.get("/html/assets/the.xml")})));



RaniaWael 

  • SheryBulos  Can you try with below java script code

    <@liaAddScript>
    	;(function ($) {
    		$.ajax({
               url : '${asset.get('/html/assets/companyProducts.xml')}',
               type: 'GET',                    
               success : function(data){
               		console.log(data);
               }, 
    	       error : function(data){
    	        
    	       }
    
            });
    	})(LITHIUM.jQuery); 
    </@liaAddScript>

     

  • I don't think you can parse XML with Freemarker by itself.

    Freemarker is Server-side and JavaScript is client-side, so passing information back and forth between them is difficult. 

    If you fetched the XML with JS as described above, you could pass the information from it back to the community with and Endpoint. 

    If you are able to get the data as Json, Freemarker can parse that natively with ?eval. (Note that asset.get just returns a path string, not file contents). 

    Another helpful tool could be using the http.client context object to make HTTP requests to get file contents. This can only be done in the page init script or in an endpoint (recommended).

  • MattV's avatar
    MattV
    Khoros Staff

    I don't think you can parse XML with Freemarker by itself.

    Freemarker is Server-side and JavaScript is client-side, so passing information back and forth between them is difficult. 

    If you fetched the XML with JS as described above, you could pass the information from it back to the community with and Endpoint. 

    If you are able to get the data as Json, Freemarker can parse that natively with ?eval. (Note that asset.get just returns a path string, not file contents). 

    Another helpful tool could be using the http.client context object to make HTTP requests to get file contents. This can only be done in the page init script or in an endpoint (recommended).

  • SheryBulos  Can you try with below java script code

    <@liaAddScript>
    	;(function ($) {
    		$.ajax({
               url : '${asset.get('/html/assets/companyProducts.xml')}',
               type: 'GET',                    
               success : function(data){
               		console.log(data);
               }, 
    	       error : function(data){
    	        
    	       }
    
            });
    	})(LITHIUM.jQuery); 
    </@liaAddScript>

     

    • SheryBulos's avatar
      SheryBulos
      Ace

      Hello saikumarn ,

      first of all thank you so much for your help. I tried it its working but how can I pass the data I got to the macro as  parameter ?