cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing XML response of an external web service in webscript

a_kholodkov
Champ in-the-making
Champ in-the-making
Hello,

I`ve been unsuccesfully trying to do a really general thing in Alfresco two days already    :x  - to parse XML-response of an external web service and show it in Share.
I have made a custom page in Share to show result, set up endpoint for the web service, written a web script etc. Everything works fine.

But I cannot parse XML-output of the WS. I can show fully XML-output without parsing, but I want to use only part of the data. So I want to parse it.

I do the next (this is simplified variant).

1). In *.js

var xmlRespTest = "<book><page>1</page><page>2</page><page>3</page></book>";
model.data = xmlRespTest;

2). In *ftl

<#assign dom = data.xmlNodeModel>

Then I have error message:
freemarker.template.TemplateException - Expected hash. data evaluated instead to freemarker.template.SimpleScalar

Can anybody help me? What do I do wrong?

Thanks in advance!
11 REPLIES 11

abarisone
Star Contributor
Star Contributor
Hi,
try to follow this http://freemarker.sourceforge.net/docs/xgui_imperative_learn.html since it may be useful for you needs.

Regards,
Andrea

a_kholodkov
Champ in-the-making
Champ in-the-making
Andrea, thank you.

But I had tried. This didn`t work for me. As I use JavaScript control script to request web service, not Java-backed.
In this example is used Java for "Putting the XML into the data-model".

I know I can use Java, but I want to proceed so simple task without Java-coding using embedded tools of Surf.

a_kholodkov
Champ in-the-making
Champ in-the-making
I have found the solution!

This is using "stringUtils.parseXMLNodeModel" method in *.js:

var xmlDocModel = stringUtils.parseXMLNodeModel(xmlResp);
model.doc = xmlDocModel;

Then it is possible to proceed any required operation on XML including using XPath in *.ftl:

<#ftl ns_prefixes={ "soap":"http://schemas.xmlsoap.org/soap/envelope/"}>
           
<#list doc["/soap:Envelope/soap:Body/getCasesResponse/getCasesResult/processes/process"] as p>
<h2>${p.processId}</h2>
</#list>

Hope this will be helpfully for anybody.

Regards,
Anton

Hi,

I am having trouble to get this working.
It seems my conversion to string doesn't work.

I need to read a xml file, get some tags and create spaces in my structure.
I am doing some tests. Like this:


var doc = document.content; // my uploaded xml
var doc_str = new String(doc);

var nfeProc = new XML("<nfeProc><teste>35</teste></nfeProc>"); // a xml test

//var nfeProc = new XML(doc_str.substring(0, doc_str.length));

var str_imp = nfeProc.teste ;

var logFile = space.childByNamePath("log.txt");
if (logFile == null)
{
   logFile = space.createFile("log.txt");
}
logFile.content = "";


logFile.content =str_imp;
logFile.save();




When I create a XML file in my javascript I can manipulate the tags and it works ok. But when I try to do the same with my imported xml file it doesn't work. I compared types but they are not the same.

Can you help me?

Thanks for your attention

kaynezhang
World-Class Innovator
World-Class Innovator
You don't need to convert it to string ,just write as following .it will work

var fileContent = document.content;
var xml = new XML(fileContent);

And please remeber xml stands for root element,if the content of your document is

<nfeProc><teste>35</teste></nfeProc>

and you want to get value of teste,you can just write like this

var fileContent = document.content;
var xml = new XML(fileContent);
xml.teste

Hi,

Thanks for your help.

I tried this but it returns this (the entire xml content) and not "35".


<nfeProc>
  <teste>35</teste>
</nfeProc>


I had this result from these 2 tests:

1)
var fileContent = document.content;
var xml = new XML(fileContent);
xml.teste;


2)
var xml = new XML("<nfeProc><teste>35</teste></nfeProc>");
xml.teste;


I don't know whatelse to do to get the tag content.

Am I doing something wrong?
Should I have to install something?

Thanks

You don't need to install anything,plese paste your xml content of your document  here.

valfontanette
Champ in-the-making
Champ in-the-making
Hi,
My xml file is attached.

My script is like this:



var fileContent = document.content;
var xml = new XML(fileContent);


After this I am not able to navigate throw xml content. So, I had tried with a simple xml file (the one I used before)




Thanks for your help.

kaynezhang
World-Class Innovator
World-Class Innovator
For your xml sample content

<nfeProc>
   <NFe>
      <infNFe>
         <ide>
            <cUF>35</cUF>
         </ide>
      </infNFe>
   </NFe>
</nfeProc>

if you want to get value of  /nfeProc/NFe/infNFe/ide/cUF (value is 35),you should write it like this
 var cUF = xml.NFe.infNFe.ide.cUF;


Following is sample code ,I also add some log information which will print in console for you to check if it works.
It works fine in my test envirenmont

logger.getSystem().out("####BEGIN  #####  " );
var fileContent = document.content;
var xml = new XML(fileContent);
logger.getSystem().out("#### XML CONTENT  #####  " + xml);
var cUF = xml.NFe.infNFe.ide.cUF;
logger.getSystem().out("#### cUF value is ####  " + cUF);