Simple Parsing of XML File

Hello Everyone,

i have a little Problem here, and i didn’t find the answer in any previous Topics.

Here is my Problem:
I have to Parse an XML File to Filemaker and i can’t use Filemakers import XML Function for some reason…

Here is an example of my File:

<?xml version="1.0" encoding="iso-8859-1"?>
<allg>
 <text format="tagged" config="rohtext">
<![CDATA[
Und die Kuh ist vom Eis - Qohne Fleiss kein Preis Q&Tel.; 06024 / 9701    ]]>
</text>
 
<text format="tagged" config="rohtext">
<![CDATA[
jux und dollerei in den tollen QTagen in Augsburg.www.wir-lassen-es-knallen.de    ]]>
</text>

<text format="tagged" config="rohtext">
<![CDATA[
Verkaufe 100 Truthähne, die Qnoch von Weihnachten übrig Qsind. Angebote unter &Tel.; 06182 / Q3456 ab 17 Uhr.    ]]>

</allg>

All i need from this file is what is written in between “”.

The Result should look like this:


Und die Kuh ist vom Eis - Qohne Fleiss kein Preis Q&Tel.; 06024 / 9701    
jux und dollerei in den tollen QTagen in Augsburg.www.wir-lassen-es-knallen.de    
Verkaufe 100 Truthähne, die Qnoch von Weihnachten übrig Qsind. Angebote unter &Tel.; 06182 / Q3456 ab 17 Uhr.    

How can i extract the Data within the Tags?

Tanks for your Help
Sebman

BTW: I am scripting on OS9 not X!

Something like this should work:

set xml to (read alias "Disk o' Dan:Path_to_file:xmlfile.txt")

set {saveDelim, text item delimiters} to {text item delimiters, "[CDATA["}
set lst to text items 2 through -1 of xml
set text item delimiters to "]]"
set s to ""
repeat with itm in lst
	set s to s & (text item 1 of itm)
end repeat
set text item delimiters to saveDelim
s

Hope that’s something like what you’re looking for…

  • Dan

Thanks Dan for the quick answer.

Your example works very well, but somehow i can’t get it to work in my whole skript. I always get the message “Finder got an error: can’t get text item delimiters” altough i can see in the event log that the file is read in correctly.

I attached my skript, maybe you can help me finding my mistake:

I use a Filemaker file to tell the skript which xml files need to be processed. This works quite well and i can see that all files are read in correctly… but they are not processed.

tell application "Finder"
	set this_file to (((path to desktop folder) as text) & "Schwabenecho.txt")
	set the target_file to this_file as text
	
	tell application "FileMaker Pro"
		go to database "Schwabenecho.fp5"
		set dateiname to cell "Dateinamen" of layout "Dateinamen" as list
	end tell
	
	repeat with currentword in dateiname
		if (currentword as text) is not "" then
			
			try
				set xml to (read alias (((path to desktop folder) as text) & (currentword as text)))
				set {saveDelim, text item delimiters} to {text item delimiters, "[CDATA["}
				set lst to text items 2 through -1 of xml
				set text item delimiters to "]]"
				set s to ""
				repeat with itm in lst
					set s to s & (text item 1 of itm)
				end repeat
				set text item delimiters to saveDelim
				
				set the open_target_file to open for access file target_file with write permission
				write s to the open_target_file starting at eof
				close access the open_target_file
			end try
			
		end if
	end repeat
end tell

Do i need to tell the finder that xml is of the type text? I hope i am not bothering you too much with my incompetence :wink:

Sebman

Sebman -

Since you’re in a Finder tell block, try prepending “AppleScript’s” to every instance of “text item delimiters”, for example

set {saveDelim, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}

and wherever else “text item delimiters” appears. Please let me know if that doesn’t do it…

  • Dan

Perfect! Now it work!

Thank you very much! (altough i must admit that i have no idea why i have to tell the finder that i mean “his” delimiters…)

Thanks again for your help
Sebman

Yeah, I know what you mean - I don’t know either, unless maybe “text” is meaningful to Finder and “item delimiters” throws it off or something.

You’re very welcome -

  • Dan