Page 1 of 2
Parsing XML/Soap
Posted: Thu Jun 06, 2013 3:44 pm
by garyth123
Hi,
I have some XML data which I would like to be able to parse. It opens like this:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="..." xmlns:xsi="..." xmlns:xsd="..."><soap:Body><GetAllTeamsResponse xmlns="..."><GetAllTeamsResult><... xmlns=""><Team>
Each team has an Id, Name, Country, Stadium, and so on.
For each Team in the file I want to extract the above details.
However I get "xmlerr, can't find element" when I use eg revXMLNodeContents using whatever node eg "/Team/" or "/Team" or anything earlier.
There must be something which I am not understanding. Please can someone shine some light on this issue for me.
Thanks in advance.
Re: Parsing XML/Soap
Posted: Fri Jun 07, 2013 5:04 am
by icouto
If you post an example of your code - and perhaps a fuller example of the xml you're trying to parse - it may be easier to help you.

Re: Parsing XML/Soap
Posted: Fri Jun 07, 2013 11:04 am
by garyth123
icouto wrote:If you post an example of your code - and perhaps a fuller example of the xml you're trying to parse - it may be easier to help you.

set the httpHeaders to field "headers" of cd "getallteams"
post (fld "apifunction" of cd "getallteams") to url ("url/FootballDataDemo.asmx/op=GetAllTeams")
put it into tXMLData
put uniencode(tXMLData,"UTF8") into tXMLData
set the unicodetext of field "new" to tXMLData
the field new then contains the below
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="soap envelope url" xmlns:xsi="XML schema url" xmlns:xsd="xml schema url "><soap:Body><GetAllTeamsResponse xmlns="url"><GetAllTeamsResult><url xmlns=""><Team><Team_Id>45</Team_Id><Name>Aberdeen</Name><Country>Scotland</Country><Stadium>Pittodrie Stadium</Stadium><HomePageURL>url</HomePageURL><WIKILink>url</WIKILink></Team>...<AccountInformation>Data requested at 07-06-2013 11:43:57 from 2.102.85.35, Username: garyth123. You are correctly logged into the Demo-service, and only records from the Scottish Premier League can be extracted here. If no data is shown, no matches were found using the specified parameters. If you are using the livescore-method and no matches are returned, it is because no matches is currently being played in the Scottish Premier League.</AccountInformation></url></GetAllTeamsResult></GetAllTeamsResponse></soap:Body></soap:Envelope>
then I'm trying to create an XML tree
put revCreateXMLTree(tXML_data ,false,true,false) into sXMLID
if sXMLID is not an integer then
answer error "Failed to process preferences file with error: " & sXMLID
return empty
end if
so that I can extract info via eg revXMLNodeContents or other revXML commands.
But maybe I want to render the XML rather than parse it. Please excuse my ignorance of XML/SOAP. Any advice would be a great help.
Somehow I am unable to post to the forums with links or domain/page references included in my post.
Re: Parsing XML/Soap
Posted: Fri Jun 07, 2013 12:58 pm
by garyth123
Actually I am not able to parse the most simple XML as in the Academy tutorial on Processing XML. So with even this code and the XML address book as in the tutorial
local sXMLID
on mouseUp
createTree
populateFields
end mouseUp
on createTree
put url "file: C:\Program Files (x86)\RunRev\LiveCode Community 6.0\Training materials\my scripts and others\myaddressesbook.xml" into tXMLData
put revCreateXMLTree(tXMLData, true, true, true) into sXMLID
end createTree
tXMLData is empty.
Re: Parsing XML/Soap
Posted: Fri Jun 07, 2013 10:40 pm
by Simon
Hi Garyth,
I haven't got a proper solution for you but I do have a workaround that will at least get you going:
Code: Select all
put uniencode(tXMLData,"UTF8") into tXMLData
put lineoffset("<Team>", tXMLData) into tLine
delete line 1 to (tLine-1) of tXMLData --delete everything above <Team>
set the unicodetext of field "new" to tXMLData
Then for retrieving specific entries:
Code: Select all
local Team_Id
put revXMLNodeContents(pTree,"Team/Team_Id") into Team_Id
This is working from the code in the lesson:
http://lessons.runrev.com/s/lessons/m/4 ... n-xml-file
Just a few changes to work with your variable names/xml data.
I was unable to get "<url xmlns="">" to behave for me so that lineoffset just removes everything I couldn't make work.
This is just so you can work with your original xml, I hope someone can figure out the actual problem.
Simon
EDIT: opps, I forgot I formatted your xml for easy reading. The line for what you are actually receiving should be:
Code: Select all
put uniencode(tXMLData,"UTF8") into tXMLData
put offset("<Team>", tXMLData) into tChar
delete char 1 to (tChar-1) of tXMLData --delete all the characters before <Team>
set the unicodetext of field "new" to tXMLData
This all may be a bit unclear, if you have any troubles just say

Re: Parsing XML/Soap
Posted: Fri Jun 07, 2013 11:45 pm
by bn
Garyth,
try this in the example from the Academy.
replaces the createTree handler
Code: Select all
on createTree
answer file "where is the XML-file?"
if it is empty then exit createTree
put it into tPath
put "file:" & tPath into tPath
put url tPath into tXMLData
put revCreateXMLTree(tXMLData, true, true, true) into sXMLID
end createTree
it makes shure that the file path is ok.
I don't know about Windows but you have a space after "file:" before C. maybe that is the problem. Once you have it working you can hardCode the filepath again.
The script works for me on MacOS
Kind regards
Bernd
Re: Parsing XML/Soap
Posted: Sat Jun 08, 2013 12:53 pm
by garyth123
Hi bn, yes that worked--no space is needed between the colon and the filename, and the filepath should have forward slashes. Thanks for your help.
Re: Parsing XML/Soap
Posted: Sat Jun 08, 2013 12:58 pm
by garyth123
EDIT: opps, I forgot I formatted your xml for easy reading. The line for what you are actually receiving should be:
Code: Select all
put uniencode(tXMLData,"UTF8") into tXMLData
put offset("<Team>", tXMLData) into tChar
delete char 1 to (tChar-1) of tXMLData --delete all the characters before <Team>
set the unicodetext of field "new" to tXMLData
This all may be a bit unclear, if you have any troubles just say

[/quote]
Okay, thanks for this Simon I'll give it a go.
Re: Parsing XML/Soap
Posted: Sun Jun 09, 2013 6:36 pm
by garyth123
It seems that the revXML commands need the data to be properly formatted/indented. But I get the xml returned in a string without this formatting/indentation. Does anyone know if there is an easy way to do this in Livecode, or do you have to write a script to do it? Or is there some other means to getting properly formatted xml returned from my SOAP request. The requested data appears with indentation in the browser when requested by hand. Thanks, Gary
Re: Parsing XML/Soap
Posted: Sun Jun 09, 2013 7:11 pm
by Simon
Hi Gary.
I didn't need the formatting, LC can handle 1 long string. The second code I posted picked out "Team_Id 45" with no problem.
Simon
Re: Parsing XML/Soap
Posted: Sun Jun 09, 2013 7:33 pm
by garyth123
Simon wrote:Hi Gary.
I didn't need the formatting, LC can handle 1 long string. The second code I posted picked out "Team_Id 45" with no problem.
Simon
I'm getting "xmlerr can't find element". I've got a root node of <XMLSOCCER></XMLSOCCER>. I figured I needed a root node. The LC lessons/Academy pages seem to have root nodes.
Re: Parsing XML/Soap
Posted: Sun Jun 09, 2013 8:08 pm
by Simon
I don't recall Soccer in your xml sample. Can you post the full xml here?
Or would that be just doing your work for you? What does your revXMLNodeContents look like.
Oh, and you haven't said if the code I gave you worked for you.
Simon
Re: Parsing XML/Soap
Posted: Sun Jun 09, 2013 8:25 pm
by dave_probertGA6e24
Hi Gary,
I would suggest a little time spent on making the basics of the XML parsing process work for you before venturing into the SOAP handling stuff.
Try to get to grips with the structure of basic XML, with some Attributes, Text nodes and Peer level nodes. Once you fully understand what the core of any XML 'file' is then it all becomes very clear. Then the various Livecode XML tools also become quite simple to get to use.
The Lessons example is very simple really, but you will probably find that in time you will create a bunch of personal functions to handle the XML nodes in more 'useful' ways (all dependent on your specific needs).
The Livecode XML functions definitely do work, but you might need to monitor what values are being returned and if there are any error messages. There is a chance that the "soap:Envelope" (note the colon) might be messing them up. Not sure how robust the LC stuff is regarding namespacing. I'm sure someone else can comment there.
Cheers,
Dave
Re: Parsing XML/Soap
Posted: Sun Jun 09, 2013 8:29 pm
by garyth123
Okay, thanks Dave. That sounds like good advice.
Re: Parsing XML/Soap
Posted: Sun Jun 09, 2013 8:43 pm
by garyth123
Hi Simon,
I just posted a reply to your most recent post but can't see it in the thread.
Yes, your code worked and has helped me see a way forward so thank you very much for your contribution.
In the code which is returned in response to the SOAP request there is a tag <XMLSOCCER.COM xmlns=""> and I have amended this to be <XMLSOCCER> to act as a root node. I tidied up the other end of the xml string at the same time.
But revXMLContents not working for me. Damn! (Probably some silly error on my part.)