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).

5 Replies

  • 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
    5 years ago

    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 ?


  • MattV's avatar
    MattV
    Khoros Staff
    5 years ago

    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).

  • jacques_ca's avatar
    jacques_ca
    Maven
    5 years ago

    Hi MattV 

     

    Thanks a lot for clarifying this matter. You mentioned that it is possible to it via http.client request. How would you do it? Do you have an example we can apply?

    Thanks