XML Parsing and integer errors

Dear reader,

My problem du jour is that I am trying to parse an XML file so as to pull individual values from it and assign them to variables. The error I am getting is - Error location XMs/1: Can’t make “ESD MAC:FTP Server:Johnny Test:Inbox:Job Ticket_data.xml” into type integer. I don’t WANT an integer and for all I can see I am not asking for one, but then my XML skills are even less developed than my Applescript skills so maybe I am?

The following comes from my Queue Management script and is the step where the next job in the preprocessing queue gets kicked over to be run through preprocessing (checking for the existence of files, validating data, verifying users, etc.)

--> Running the preprocessing script

on sRunPreprocessing(lUserName, this_folder, lScriptsFolder, gPrefsFolder)
	if (pPreProcTime ≥ (pPreProcTime + (30 * minutes))) or (pPriorityNo = 0 and pGeneralNo = 0) then -- 30 or more minutes or no other jobs in queue
		set LoadPreprocessing to load script alias (lScriptsFolder & "Preprocessing.scpt" as Unicode text) -- Load Preprocessing script
		tell LoadPreprocessing
			sPreprocess(lUserName, this_folder, lScriptsFolder, gPrefsFolder) -- Run preprocessing script for next in queue
		end tell
		set pPreProcTime to time of (current date) -- Reset the clock to now
	end if
end sRunPreprocessing

This next bit comes from the preprocessing script and is attempting to read the XML created from an Adobe Acrobat form in preparation to verify the customer / enter new information into a database record.

--> parse .XML file (unless I went cgi route)
			
		set z to 3 -- Block marker
		set LoadXMLParser to load script alias (lScriptsFolder & "XML Parser.scpt" as Unicode text)
		tell LoadXMLParser
			set lJobType to sGetItem(gInboxFolder, lXMLFileName, "PDFJobType")
			set lUserName to sGetItem(gInboxFolder, lXMLFileName, "PDFContactName")
			set lAddress to sGetItem(gInboxFolder, lXMLFileName, "PDFCustomerAddress")
			set lCity to sGetItem(gInboxFolder, lXMLFileName, "PDFCustomerCity")
			set lState to sGetItem(gInboxFolder, lXMLFileName, "PDFCustomerState")
			set lZipCode to sGetItem(gInboxFolder, lXMLFileName, "PDFCustomerZip")
			set lToAddress to sGetItem(gInboxFolder, lXMLFileName, "PDFCustomerEmail")
			set lCCAddresses to sGetItem(gInboxFolder, lXMLFileName, "PDFCCEmailAddresses")
			set lPriority to sGetItem(gInboxFolder, lXMLFileName, "PDFPriority")
		end tell

This last bit is from my XML parser script and yes, I know it isn’t actually written to do what I need yet but it should, when I get it right, pull the value form the XML associated with lXMLLabel and assign it to a variable. Right now I would just be happy if I could get ANYTHING that indicated that data of ANY sort was being pulled from the XML File but all I seem to get are stupid integer errors. Any ideas on what I am doing wrong?

on sGetItem(gInboxFolder, lXMLFileName, lXMLLabel)
	set lXMLPath to (gInboxFolder & lXMLFileName as Unicode text)
	
	try
		set y to "XMs" -- Script designator
		set z to 1 -- Block marker
		tell "XMLLib"
			set lOpenXML to XMLOpen alias (gInboxFolder & lXMLFileName as Unicode text)
			set lXMLRecord to XMLGetText lOpenXML as record
			XMLClose lOpenXML
		end tell
	on error msg
		display dialog "Error location " & y & "/" & z & ": " & msg with icon stop
	end try
	
end sGetItem

Thanks in advance for any help rendered. I was also wondering, does anyone know where I can find some good acgi dispatcher tutorials or examples? The ones that came with aren’t helping me understand how to make it work.

Hi,

it’s very hard to look for errors only with having subroutines,
because you can’t see, which kind of value you pass thru the subroutine call.

What is this tell “XMLLib” for?
Targetting a literal string is only useful if you want parse single characters of it

Hi Stefan,

XMLLib is the OSAX from SatImage I was planning on using to handle the XML. I tried to use both the system events XML suite (nothing to guide me in the help file) and XML Tools OSAX from Late Night Software (had no better luck than with XMLLib). Realize that I know little about Applescript and less about XML so this is all a lot of stumbling around in the dark here for me but I assumed that one of the above methods would allow me to pluck a value from an associated label (probably wrong terminology) from an XML file on my hard drive. The following is the XML file data (slightly modified to make it shorter and strip out comments put in by Adobe Acrobat):

The values I am passing to the XML Parser script, sGetItem(gInboxFolder, lXMLFileName, lXMLLabel), are:

gInboxFolder = The path to the users inbox folder as unicode text (ie: “ESD MAC:FTP Folder:Johnny Test:Inbox:”)
lXMLFileName = The name of the XML file to be pulled from gInboxFolder (ie: “Job Ticket_data.xml”)
lXMLLabel = The label (seen above as ) associated with the data I wish to retrieve (ie:“PDFJobType”)
Please note that what I am trying to get at with this is the “catalog” value associated with the “PDFJobType” label

If this isn’t enough to go on please let me know what you need and I will try to get it to you if I can find it.

Hi,

I understand, I don’t use XMLLib.
For parsing not too complicated stuff, System Event’s XML parsing capabilities are sufficient.
I saved your sample code in a plain Text file (UTF-8 encoded) named “test.xml” on desktop.
With this routine you can get the value of a specified label.

set PDFJobType to sGetItem("PDFJobType", ((path to desktop as Unicode text) & "test.xml"))

on sGetItem(Label, XMLfile)
	tell application "System Events"
		tell XML element 1 of contents of XML file XMLfile
			return value of XML element Label
		end tell
	end tell
end sGetItem

If the order of the elements never changes, you can assign all values in one line like

tell application "System Events"
	tell XML element 1 of contents of XML file ((path to desktop as Unicode text) & "test.xml")
		set {PDFJobType, PDFCustomerCompany, PDFCustomerAddress, PDFCustomerCity, PDFCustomerZip, PDFCustomerCountry, PDFCustomerPhone, PDFCustomerFax, PDFContactName, DFCustomerState, PDFCustomerEmail} to value of XML elements
	end tell
end tell

But unfortunately I couldn’t see, where the integer error occurs

With XMLLib, I think you need Smile. e.g.


set f to choose file
tell application "Smile"
	set xml_ref to XMLOpen f
	set xml_parent to XMLChild xml_ref index 1
	set xml_child to XMLFind xml_parent name "PDFCustomerCity"
	set t to XMLGetText xml_child
	XMLClose xml_ref
end tell
t

gl,

Stefan,

I tried your code but now the error is ‘Error location XMs/1: NSReceiverEvaluationScriptError: 4’. On the one hand it is a NEW error and kind of like progress :smiley: , but on the other it is cryptic and of little help to me :confused: . I will do a web search on the error and see if I can find out what it means.

on sGetItem(Label, XMLfile)
	
	try
		set y to "XMs" -- Script designator
		set z to 1 -- Block marker
		tell application "System Events"
			tell XML element 1 of contents of XML file XMLfile
				return value of XML element Label
			end tell
		end tell
	on error msg
		display dialog "Error location " & y & "/" & z & ": " & msg with icon stop
	end try
	
end sGetItem

Kel,

Now that you mention it, it makes sense that I would have to call smile to do that. I will also try your suggestion… in the morning. Soooo tired. Must. sleezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

NSReceiverEvaluationScriptError: 4 means, that the UI element, you’re going to access, doesn’t exist.

If your XML file has exactly the same structure as your sample code and the
variable XMLfile is a string path to an existing file, it should work

Hi, Rectal.

The immediate cause of this error is that you’re telling the string “XMLLib” to execute the XMLLib OSAX commands. I don’t know why the error message mentions an integer, but you don’t need to ‘tell’ an OSAX to do anything. Its commands are extensions to the language while it’s installed.

I downloaded XMLLib this morning and saved a copy of your XML document on my desktop. I haven’t read XMLLib’s on-line documentation yet, but a little experimentation gives this:

on sGetItem(gInboxFolder, lXMLFileName, lXMLLabel)
	set lXMLPath to (gInboxFolder & lXMLFileName as Unicode text)
	
	try
		set y to "XMs" -- Script designator
		set z to 1 -- Block marker
		
		set lOpenXML to (XMLOpen alias (gInboxFolder & lXMLFileName)) --> A reference to the opened document.
		set rootObject to (XMLRoot lOpenXML) --> A reference to the "<form1>" object.
		set targetObject to (XMLFind rootObject name lXMLLabel) --> A reference to the child object with the given label.
		set targetTextValue to (XMLGetText targetObject) --> The text value of that object.
		XMLClose lOpenXML
	on error msg
		XMLClose lOpenXML
		display dialog "Error location " & y & "/" & z & ": " & msg with icon stop
	end try
	
	return targetTextValue
end sGetItem

sGetItem(path to desktop as Unicode text, "Job Ticket_data.xml", "PDFJobType")
--> "Catalog"

Edit: Hah! This code’s almost the same as Kel’s! Sorry, Kel. :rolleyes: But you don’t need Smile to use XMLLib.

Thought I had used XMLLib without Smile before, but now I get execution error: Satimage XML additions have to be called from Smile. Wonder why that is?

Thanks,

Hi Nigel,

I was using the old version, so updated it and now it works.

Thanks a lot,

Regarding my post from Yesterday 02:56:59

The problem , Stefan, is that I had put a label in the script (PDFPriority) but forgot to add that variable to the Acrobat form. :rolleyes:

What I ended up using is:

on sGetItem(Label, lXMLFilePath)
	
	try
		set y to "XMs" -- Script designator
		set z to 1 -- Block marker
		tell application "System Events"
			tell XML element 1 of contents of XML file lXMLFilePath
				return value of XML element Label
			end tell
		end tell
	on error msg
		display dialog "Error location " & y & "/" & z & ": " & msg with icon stop
	end try
	
end sGetItem

Of course since the XML parsing subroutine is now working perfectly I just HAD to go into the queue management script like some crack addicted monkey and break it! I can’t have anything nice.

Thanks to everyone and I will be keeping a copy of this post since the XMLLib looks like the way to go when I need to get a little more creative with XML files. Also good to know that an OSAX does not need to be “told”. Of course now I will need to go into the 15 or so scripts I wrote/am writing for this project and fix those tells. I never thought to close an open document on error but it seems so obvious now, thanks.