Parsing XML/Soap

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Parsing XML/Soap

Post by garyth123 » Thu Jun 06, 2013 3:44 pm

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.

icouto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 92
Joined: Wed May 29, 2013 1:54 am

Re: Parsing XML/Soap

Post by icouto » Fri Jun 07, 2013 5:04 am

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

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Parsing XML/Soap

Post by garyth123 » Fri Jun 07, 2013 11:04 am

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.

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Parsing XML/Soap

Post by garyth123 » Fri Jun 07, 2013 12:58 pm

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.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Parsing XML/Soap

Post by Simon » Fri Jun 07, 2013 10:40 pm

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 :)
Last edited by Simon on Fri Jun 07, 2013 11:53 pm, edited 1 time in total.
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Parsing XML/Soap

Post by bn » Fri Jun 07, 2013 11:45 pm

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

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Parsing XML/Soap

Post by garyth123 » Sat Jun 08, 2013 12:53 pm

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.

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Parsing XML/Soap

Post by garyth123 » Sat Jun 08, 2013 12:58 pm

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.

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Parsing XML/Soap

Post by garyth123 » Sun Jun 09, 2013 6:36 pm

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Parsing XML/Soap

Post by Simon » Sun Jun 09, 2013 7:11 pm

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 used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Parsing XML/Soap

Post by garyth123 » Sun Jun 09, 2013 7:33 pm

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.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Parsing XML/Soap

Post by Simon » Sun Jun 09, 2013 8:08 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Parsing XML/Soap

Post by dave_probertGA6e24 » Sun Jun 09, 2013 8:25 pm

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
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Parsing XML/Soap

Post by garyth123 » Sun Jun 09, 2013 8:29 pm

Okay, thanks Dave. That sounds like good advice.

garyth123
Posts: 88
Joined: Sat Apr 27, 2013 11:14 am
Contact:

Re: Parsing XML/Soap

Post by garyth123 » Sun Jun 09, 2013 8:43 pm

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

Post Reply