Parsing XML response of an external web service in webscript
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2013 04:44 AM
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!
- Labels:
-
Archive
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2013 04:53 AM
try to follow this http://freemarker.sourceforge.net/docs/xgui_imperative_learn.html since it may be useful for you needs.
Regards,
Andrea
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2013 11:46 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2013 03:57 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2014 03:06 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2014 11:59 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2014 11:56 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2014 12:04 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2014 04:17 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2014 08:06 PM
<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);
