Page 1 of 1

Can't extract double quoted text from XML?

Posted: Wed Aug 03, 2011 12:34 am
by BarrySumpter
I have this data in lblFileContents:

Code: Select all

...
<period>Monthly</period>
<fee unit="AUD$">999.99</fee>
...


I call the function this way:

Code: Select all

put ExtractTagData("period") into strperiod   --works
put ExtractTagData("fee unit=" & quote & "AUD$" & quote,"Fee" ) into strFee  -- does NOT work


And the function works with anything but embedded double quotes:
(note:
The replace function finds the embedded double quotes without issue.
But for some reason the wordOffset function does NOT)

Code: Select all

Function ExtractTagData tagName, tagEndName
   
   put field lblFileContents into tagData
   
   if tagEndName is Empty then
      put tagName into tagEndName
   end if
   
   replace "<" & tagName & ">" with " <" & tagName & "> " in tagData      -- this replace finds the fee with double quotes
   replace "</" & tagEndName & ">" with " </" & tagEndName & "> " in tagData
   
   Put wordOffset("<" & tagName & ">",tagData) into tWordStartNum       -- this wordOffset DOES NOT find the fee with double quotes
   Put wordOffset("</" & tagEndName & ">",tagData) into tWordEndNum
   
   Delete word tWordEndNum to -1 of tagData
   Delete word 1 to tWordStartNum of tagData
   
   return tagData
   
end ExtractTagData
Any positive constructive suggestions would be greatly appreciated.

Re: Can't extract double quoted text from XML?

Posted: Wed Aug 03, 2011 1:40 am
by Dixie
Assuming that

<period>Monthly</period>
<fee unit="AUD$">999.99</fee>

is in fld 1 for this example, then...

Code: Select all

on mouseUp
   put fld 1 into theTagData
   put theFee(theTagData)
end mouseUp

function theFee theDataToExtract
   delete line 1 of theDataToExtract
   put lineOffset("<fee unit=",theDataToExtract) into theLine
   replace "<fee unit=" with empty in line theLine of theDataToExtract
   repeat with count = the number of chars of  theDataToExtract down to 1
      if char count of theDataToExtract = quote then delete char count of theDataToExtract
   end repeat
   replace "</fee>" with empty in line theLine of theDataToExtract
   replace ">" with "=" in line theLine of theDataToExtract
   return theDataToExtract
end theFee
will return :- AUD$=999.99

Re: Can't extract double quoted text from XML?

Posted: Wed Aug 03, 2011 4:54 am
by BarrySumpter
Dixie rules!

Thanks so much for the excellent post.
Doing stuff I've never seen.
Nice.

Man! Thats a lot of work.

I should have thought about the replace the unit with empty.
I think unit is called an attribute and the fee is called a tag.

Code: Select all

replace "fee unit=" & quote & "AUD$" & quote with "fee" in lblFileContents 
then

Code: Select all

put ExtractTagData("fee") into strFee   --works
Unless I actually NEED that currency type later. filter something. i'll experiment later.
But for now it's just for me.

----

Also I can never remember if "" is the same in livecode as null or empty or blank.
Its empty.
And I can never remember type casting.
The one that bugs me the most is
... the label of field label1
I always want to do
.. the lable of label label1
I'm not even sure I got that right. LOL.

I'll have to write a cheat sheet an leave in on me wall.
Unless someone already has a cheat sheet they use and don't mind sharing.