XML Question

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
eworthington
Posts: 7
Joined: Tue May 14, 2019 5:12 am

XML Question

Post by eworthington » Fri Nov 08, 2019 12:19 am

I have been trying to work through an XML document to pull out specific data points.

I have been able to use revXMLChildNames to iterate a few layers deep, but when I try to go further, it seems to fail (it returns nothing).

This is the loop in question that is producing errors.

repeat for each line L in tComponents
repeat for each line E in tEntries
put revXMLChildNames(tXML,"ClinicalDocument/component/structuredBody/" & \
L & "/section/" & E,return,"act",True) into tAct
end repeat
end repeat

To get the values for L and E, I used the following code that work.

--This is the initial breakdown of the document. It creates a separate tComponents node - tComponents[n]
--for each instance.
put revXMLChildNames(tXML,"ClinicalDocument/component/structuredBody",return,"component",True) into tComponents
--This is the second breakdown of the document. It creates a separate tEntries node - tEntries[n] for
--each instance.
repeat for each line L in tComponents
put revXMLChildNames(tXML,"ClinicalDocument/component/structuredBody/" & \
L & "/section" ,return,"entry",True) into tEntries
end repeat

I was wondering if someone had some advice. I have limited experience with LiveCode (a few months), and this is my first project with the XML library.

cutt<dot>ly/CeYk4VJ

Above is a link to the stack and a test XML file.

Thank you for any help. Ultimately, I want to try to iterate through the document and pull specific attributes and values out, but I need to figure out how to traverse it efficiently.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2846
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: XML Question

Post by mwieder » Mon Nov 11, 2019 8:20 pm

See if this helps... note that I guessed at the xml structure looking at the sample, so this may not be exactly what you're looking for, but it should get you started. The same applies because the file in the zip archive is not the same as the one your code references. YMMV.

Code: Select all

on mouseUp
   local tFile
   local tXML
   local tParent
   local tComponent
   local tNumChildren
   
   put URL("file:" & specialfolderpath("documents") & \ 
   "/livecode_qrda/EH_Sample_QRDA_I_2019-Informative.xml" ) into tFile
   
   put revXMLCreateTree(tFile,False,True,False) into tXML
   
   put "/ClinicalDocument/component/structuredBody/" into tParent
   put revXMLNumberOfChildren(tXML, tParent, "component", -1) into tNumChildren
   put revXMLFirstChild(tXML, tParent) into tComponent # first component
   
   local tNumLOINC=0
   local tEntry, tObservation
   local tSystemName, tValue
   repeat while tComponent is not empty # 4 components
      if "xmlerr" is in tComponent then
         exit repeat
      end if
      put revXMLFirstChild(tXML, tComponent & "/section") into tEntry
      repeat while tEntry is not empty
         if "xmlerr" is in tEntry then
            exit repeat
         end if
         put revXMLFirstChild(tXML, tEntry & "/observation") into tObservation
         repeat while tObservation is not empty
            if "xmlerr" is in tObservation then
               exit repeat
            end if
            put revXMLAttribute(tXML, tObservation, "codeSystemName") into tSystemName
            if "LOINC" is in tSystemName then
               add 1 to tNumLOINC
            end if
            put revXMLNextSibling(tXML, tObservation) into tObservation
         end repeat -- while tObservation is not empty
         put revXMLNextSibling(tXML, tEntry) into tEntry
      end repeat -- tEntry is not empty
      put revXMLNextSibling(tXML, tComponent) into tComponent
   end repeat -- while tComponent is not empty
   
   answer tNumLOINC && "LOINC entries"
end mouseUp

eworthington
Posts: 7
Joined: Tue May 14, 2019 5:12 am

Re: XML Question

Post by eworthington » Sat Nov 16, 2019 5:15 pm

Thank you very much. I will try the code out.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”