Reading XML not from file.

Hello,

I am doing a script that needs to parse XML info that I get from a command line call. I am able to parse an xml when it comes from a file, but it doesn’t seem to work so well from with XML info is passed from a do shell script.

The following is just a portion of a script.



repeat with i in paragraphs of z
	set theAssetXMl to (do shell script x & "dataapp getmd --xml /asset/" & theAsset) as string
	my myREADXML(theAssetXMl)
	display dialog mytitle
	
end repeat

on myREADXML(theAssetXMl)
	
	
	tell application "System Events"
		
		set mytitle to ""
		set assetID to ""
		set status to ""
		set creationDate to ""
		tell XML element "values" of XML element "sessions" of XML data theAssetXMl
			
			repeat with thisElement in XML elements
				tell thisElement
					if value of XML attribute "fieldName" is "CUST_TITLE" then
						set mytitle to its value as text
						
					end if
				end tell
			end repeat
		end tell
	end tell
	
end myREADXML



So what happens is the script goes and shell script queries the commandline application for a certain Asset.

The commandline app returns XML data like so (I have shortened it, because it is a lot longer):

<?xml version="1.0"?> 2009-03-09T17:29:34Z Test Me Self Pity

Anyway, this currently doesn’t work. However if I start from an XML file first (like in the snippet below…also not the full script because it is very long), the XML parsing would work.


on myREADXML(myFile)
	tell application "Finder"
		set xmlFile to POSIX path of myFile
	end tell
	
	tell application "System Events"
		
		set mytitle to ""
		set assetID to ""
		set status to ""
		set creationDate to ""
		tell XML element "getMdReply" of XML element "Dataapp" of XML file xmlFile
			tell XML element "metadata" of XML element "entity"
				repeat with thisElement in XML elements
					tell thisElement
						if value of XML attribute "fieldName" is "Title" then
							set mytitle to its value as text
							
						else if value of XML attribute "fieldName" is "Asset ID" then
							set assetID to its value as text
						else if value of XML attribute "fieldName" is "Status" then
							set status to its value as text
						else if value of XML attribute "fieldName" is "Creation Date" then
							set creationDate to its value as text
						end if
					end tell
				end repeat
			end tell
		end tell
	end tell
end myREADXML



So, can you parse XML info without an actual XML file, just passed data?

I should note that the XML parsing at the bottom isn’t for the XML material I’m getting from the commandline script, even if I save it to a file. It’s just an example of an XML parsing script that worked ok for me.

Well, I’m having issues with the XML even when I read it from the file. :confused: The way to use System Events properly with XML seems to be pretty opaque.

I guess for the commandline script I can write out the XML by just doing a > /tmp/myTempXML.xml.

So, maybe starting from the file would be fine.

This is the whole XML file.

<?xml version="1.0"?> 3367004 2009-03-14T04:43:08Z betterresults.mov 2009-02-11T02:38:42Z 2009-02-11T02:38:10Z true 00:00:30:00 admin 0.891346 2009-02-11T02:38:09Z video_clip Commercial The Big Drivef Ready for Review 848,480 1291 23994/1001 pa_asset_media Other mpeg4_aac 220 video/quicktime true 01:03:00:01 h264 2009-03-09T17:21:55Z Bill Better Results 2009-03-09T17:21:55Z true video

So, I’m kind of starting from scratch here.

This is what I have so far.



choose file without invisibles
set xml_File to result as Unicode text

tell application "System Events"
	tell XML element 1 of contents of XML file xml_File 
		
		tell XML element "values" to set myList to value of the XML attributes of XML elements
		
		
	end tell
end tell

-- Results in: {{"CUST_SIZE"}, {"DB_LAST_FETCHED"}, {"CUST_LOCATION"}, {"PA_MD_CUST_FILENAME"}, {"LAST_ANALYSED"}, {"ENTITY_CREATED"}, {"VERSION_ASSET"}, {"ASSET_DURATION"}, {"ENTITY_CREATE_USER_NAME"}, {"ASSET_VIDEO_BIT_RATE"}, {"CUST_CREATED"}, {"ASSET_MEDIA_TYPE"}, {"CUST_DESCRIPTION"}, {"CUST_DEVICE"}, {"CUST_ASSET_STATUS"}, {"ASSET_IMAGE_SIZE"}, {"DB_ENTITY_ID"}, {"ASSET_VIDEO_FRAME_RATE"}, {"ASSET_GUID"}, {"ASSET_TYPE"}, {"CUST_CATEGORY"}, {"ASSET_AUDIO_CODEC"}, {"ASSET_NUMBER"}, {"ASSET_WRAPPER_FORMAT"}, {"ENTITY_IS_LOCAL"}, {"CUST_KEYWORDS"}, {"ASSET_TIMECODE_OFFSET"}, {"ASSET_VIDEO_CODEC"}, {"MD_LAST_MODIFIED"}, {"CUST_OWNER"}, {"CUST_TITLE"}, {"LAST_ACCESSED"}, {"PA_MD_CUST_SHOW_SYNOPSIS"}, {"PA_MD_CUST_SEND_TO_MAC_MINI"}, {"ASSET_VIDEO_ELEMENTS"}}


So, now I want to be able to pick out like five values to create a report (let’s start with two to start… “CUST_TITLE” and “CUST_DESCRIPTION”). How do I get the data that goes with those fields?

Cheers,

Anton

Hi,

something like this


set xml_File to (choose file without invisibles) as text

tell application "System Events"
	tell XML element 1 of contents of XML file xml_File
		tell (1st XML element of XML elements whose id is "CUST_TITLE")
			set a to value of XML element 1 as text
		end tell
		tell (1st XML element of XML elements whose id is "CUST_DESCRIPTION")
			set b to value of XML element 1 as text
		end tell
	end tell
end tell

That worked like a charm. The key was the how the ID was used.

I was trying a whole bunch of different things…like this below (which didn’t work)…and many different variations. Pretty frustrating.



tell XML element 1 of contents of XML file xmlFile
		-- First element whose name is "objectA". You could also use XML element 1
		tell XML element "values" to set myList to name of the XML attributes of XML elements
		
		repeat with thisElement in XML elements of XML element "values"
			tell thisElement
				try
					if value of XML attribute "id" is "CUST_SIZE" then
						set mytitle to its value as text
					end if
				end try
			end tell
		end repeat
		
		display dialog mytitle
	end tell

And it was endlessly frustrating.

It’s very simple when you see the script you wrote…but I could find any kind of documentation anywhere for that kind of syntax.

Thanks again.

A.

Almost everything is trial and error.
The XML elements are nested, I usually log certain values to see where I am