Simple XML Parse into variable

Learning Applescript and have an application. I’ve done a lot of searching and I feel I am missing something that may be fairly obvious.

I have an XML file that is going to be used for VPN configuration (or at least initiation). The file needs to be chosen outside of the script. The XML is formatted like this:

<?xml version="1.0" encoding="UTF-8"?> 10.1.20.0 255.255.255.0 1.0.0.1 VPN

I want to be able to pull the values of remoteNetwork, remoteSubnet, vpnAddress, and vpnName and use them elsewhere in the script.

here’s what I have so far:


set xmlData to ((choose file) as text)

tell application "System Events"
	tell XML element "vpnConfig" of contents of XML file xmlData
		set vpnName to (value of XML element "vpnName")
		set remoteNetwork to (value of XML element "remoteNetwork")
		set vpnAddress to (value of XML element "vpnAddress")
		set remoteSubnet to (value of XML element "remoteSubnet")
	end tell
end tell

tell application "System Events"
	set VPN to service vpnName
	if exists VPN then connect VPN
end tell

I am going to use the other variables to create a static route for the connection, but I have yet to get to that part. When I run this, I choose the file and end up getting an error of:

error “System Events got an error: Can’t get XML element "vpnConfig" of contents of XML file "Macintosh HD:Users:Farroar:Desktop:vpnXML.xml".” number -1728 from XML element “vpnConfig” of contents of XML file “Macintosh HD:Users:Farroar:Desktop:vpnXML.xml”

I’ve tried various combinations of examples I’ve found online and nothing seems to get past this portion. The data is obviously not being handed down into the tell block or I’m not parsing it correctly.

Also, I am looking for guidance on calling these variables elsewhere but I’ll get to that when I can actually get these variables their data!

Thanks!

So, I may have gotten a bit further with this but I’m getting a new error. Here’s my updated script, realized that maybe I should try to work on the opening of the file since that is where it seems to be breaking:


set configFile to (choose file)
open for access configFile
set xmlData to (read configFile)
close access configFile

tell application "System Events"
	tell xmlData
		tell XML element "vpnConfig"
			set vpnName to (value of XML attribute "vpnName")
			set remoteNetwork to (value of XML attribute "remoteNetwork")
			set vpnAddress to (value of XML attribute "vpnAddress")
			set remoteSubnet to (value of XML attribute "remoteSubnet")
		end tell
	end tell
end tell

tell application "System Events"
	set VPN to service vpnName
	if exists VPN then connect VPN
end tell

But now I’m getting the error:

error “Can’t make "vpnConfig" into type integer.” number -1700 from “vpnConfig” to integer

so there seems to be some issue with variable types?

Hi,

the xml text and the script are correct. Check the contents of the file once again. Maybe the first character is missing.

In reply to your second post:

Read a file into a string and parse the string with system events should be done like this:

set configFile to (choose file)
set XMLData to read configFile as «class utf8»

tell application "System Events"
	set rootXML to make new XML data with properties {text:XMLData}
	tell (XML element "vpnConfig" of rootXML)
		set vpnName to (value of XML element "vpnName")
		set remoteNetwork to (value of XML element "remoteNetwork")
		set vpnAddress to (value of XML element "vpnAddress")
		set remoteSubnet to (value of XML element "remoteSubnet")
	end tell
end tell

return {vpnName, remoteNetwork, vpnAddress, remoteSubnet}

The XML file looks fine to me. I did see an issue where since I created it in a text editor that the file was saved in .rtf and a bunch of unnecessary stuff was hidden in the file. I took all of that out and am not using an XML editor. I made the following adjustment:



set configFile to (choose file)
open for access configFile
set xmlData to (read configFile)
close access configFile

tell application "System Events"
[b]	tell XML element "vpnConfig" of contents of XML file xmlData
[/b]		set vpnName to (value of XML element "vpnName")
		set remoteNetwork to (value of XML element "remoteNetwork")
		set vpnAddress to (value of XML element "vpnAddress")
		set remoteSubnet to (value of XML element "remoteSubnet")
	end tell
end tell

tell application "System Events"
	set VPN to service vpnName
	if exists VPN then connect VPN
end tell

Which is now resulting in the following error:

error “System Events got an error: Can’t get XML file "<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>

10.1.10.0
255.255.255.0
1.1.1.1/vpnAddress>
VPN
".” number -1728 from XML file “<?xml version=\"1.0\" encoding=\"UTF-8\"?>

10.1.10.0
255.255.255.0
1.1.1.1
VPN

So, it’s at least reading the file but I’m not getting what this error is saying.

Bing-o DJ Bazzie Wazzie!

So, do I even need to open / close the file to begin with or just set the contents as you specified?

Also, if I need to call these variables elsewhere, so I just call them by the name or is there a different method? I’m going to be calling them in a few different tell blocks I think.

Thanks!

The text file is probably messed up.

Make a new one

¢ Launch TextEdit
¢ Press ⇧⌘T to set the format to plain text
¢ Copy and paste the xml text from your initial post into the text view
¢ Save the text with an .xml extension.

Then the first script will work

Nice! :cool:

When you use the read command followed by a file path the file will be opened, read and closed and the read data will be returned. opening a file and using a file descriptor is useful to read files in chunks or parts of the file even in different formats. But since we want to read the entire file as string we can simply use the read command.

The variables are withing the scope of the script, the meaning of the variable doesn’t change in another tell block unless the tell block is another script object.

Awesome… but weird thing…

It was working fine, then I added some do shell entries in order to adjust routing table entries and it stopped working. Went ahead and removed those entries but still having an error:

error “End of file error.” number -39 from file “Macintosh HD:Users:Farroar:Desktop:PVCCvpnXML.xml”

I did make a change to the .xml file but it was only an adjustment to one of the fields. What does this error mean?

Do you have the open for access or close access commands still in your script? Do you have the file open in an external editor? Since you have used open for access on those commands I recommend restarting your system or at least Script Editor and see if it’s working again. Error -39 is most of the time that a previous run (or another script) didn’t handle the file properly. The next run the error will appear.

I closed out the script editor and re-opened which fixed it. Thanks for that tip!

DJ, thanks for sharing such an excellent solution. :cool:
Provides several great examples.
I have clipped into my snippet tool (Quiver) for reference and future use.