Welcome to Programming LiveCode Q&A, where you can ask questions and receive answers from other members of the community.

Please, ask questions about the book only. If possible, mention section or page number. Don't ask for opinions, ask clear and direct questions only.

Go to main site

Quick Links

Frequently Asked Questions

Errata (1st print)

How to parse Xml Data in Live code

+1 vote

on mouseUp
end mouseUp

command loadPreferences
   local tTree
   put readPreferencesToXMLTree() into tTree
   if tTree is empty then
      exit loadPreferences
   end if
   processPreferencesTree tTree
   revDeleteXMLTree tTree
end loadPreferences

private function readPreferencesToXMLTree
   set the itemDelimiter to slash
   local tPreferencesFile
   put the URL"http://htp2.hitecpoint.in:98/api/blackbox/live/45" into tPreferencesFile
   local tPreferencesData, tResult
   put tPreferencesFile into tPreferencesData
   put the result into tResult
   if tResult is not empty then
      answer error "Failed to read preferences file at location: " & tPreferencesFile
      return empty
   end if
   local tTree
   put revCreateXMLTree(tPreferencesData, false, true, false) into tTree  
   if tTree is not an integer then
      answer error "Failed to process preferences file with error: " & tTree
      return empty
   end if
   return tTree
end readPreferencesToXMLTree

private command processPreferencesTree pTree  
  local tPosts
   put revXMLChildNames(pTree, "ArrayOfLiveStatus/LiveStatus", return, "Location", true) into tPosts    
   local tListOfNames
   repeat for each line tName in tPosts
      put revXMLNodeContents(pTree, "ArrayOfLiveStatus/LiveStatus" & tName) & return after tListOfNames
   end repeat
   delete the last char of tListOfNames  
   local tOutput
   put tListOfNames after tOutput
   set the text of field "NameList" to tOutput
end processPreferencesTree

I am using Livecode software.I am accessing web api "http://htp2.hitecpoint.in:98/api/blackbox/live/45" which return a xml data.I want to parse location node but I always get a error"xmlerr, can't find element".What I am doing wrong please help.

asked Nov 25, 2013 by vishal (130 points)
edited Nov 25, 2013 by mark

1 Answer

0 votes

The server returns JSON by default. Before you make the request, you need to set the content type to application/xml:

   set the httpHeaders to "Content-Type: application/xml"

Now the server will return XML and you should be able to retrieve the required nodes.

Let me know if this helps and accept the answer by clicking on the Accept button if it does.

answered Nov 25, 2013 by mark (3,090 points)