Read between > and <

Hi,

I am currently writing a script to edit some XML files I made a while back for another script. There’s about 2000 of them or I’d of maybe done it by hand.

The lines in the file look like this:

3244.94 3219.06 1.34

I’ve got the script reading the file and using an if clause to do the work if a paragraph matches a specific criteria but I need it to extract the data first.

Thats the first bit.

Once thats done I’d like it to take each number (3244.94 and 3219.06 and 1.34 in the above example) between the tags and set it to a variable, note, some numbers are minus numbers ie -1.34

I guess i’d pipe it through grep but my understanding of grep is limited and as much as I would like to understand it I just can’t seem to get my head around it.

Any help greatly appreciated

thanks

Dave

Maybe this code is of some help for you:


set xmltxt to "<varname>3244.94 3219.06 -1.34</varname>"
set txt to ((characters 10 through -11 of xmltxt) as Unicode text)
set stringnumbers to my getstringnumbers(txt)
set numberlist to {}
repeat with stringnumber in stringnumbers
	set numberlist to numberlist & (stringnumber as number)
end repeat
set {var1, var2, var3} to numberlist

on getstringnumbers(txt)
	set olddelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {space}
	set txtitems to text items of txt
	set AppleScript's text item delimiters to olddelims
	return txtitems
end getstringnumbers

Hey

thanks for the fast reply. This is what I actually ended up with:


set _x to (count of _paragraphs)
set i to 1
repeat until i = _x
	try
		set _curpara to paragraph i of (read file _gprofile)
	end try
	if _curpara contains "<varname>" or "</varname>" then
		-- this bit exracts X Y & Z from between <varname></varname>
		
		set Search1 to "<varname>" as string
		set Search2 to "</varname>" as string
		
		set tid to AppleScript's text item delimiters
		set text item delimiters to Search1
		set Rslt1 to text item 2 of _curpara
		set text item delimiters to Search2
		set Rslt2 to text item 1 of Rslt1 as string
		
		set _result to Rslt2 as string
		
		set AppleScript's text item delimiters to tid
		
		-- this seperates the X Y & Z
		set olddelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {space}
		set txtitems to text items of _result
		set AppleScript's text item delimiters to olddelims
		set stringnumbers to txtitems
		
		set numberlist to {}
		repeat with stringnumber in stringnumbers
			set numberlist to numberlist & (stringnumber as number)
		end repeat
		set {_x, _y, _z} to numberlist
		
		display dialog _x
		display dialog _y
		display dialog _z
		
	end if
	set i to i + 1
end repeat


Thanks again for your help and your fast response

Dave Maltby