Stripping out values from an xml file

Hi Guys and Gals

I am trying to grab three values from an xml file and with them create a new text file with them. I’m really hoping one of you can help with this.

The values I am looking to extract are “Title”, “Created” and “Text”

An example of the xml file is below

1.0 01/02/07 01:09 31/01/07 22:42

I have tried numerous scripts but either only return the name of the value and not the contents or instead get simply “”. Please please please help

Browser: Safari 535.1
Operating System: Mac OS X (10.7)

When you want the value of an element you just ask for its “value”. Try this…

set xmlText to "
<Story>
<Version>1.0</Version>
<Header>
    <Title><![CDATA[divorce]]></Title>
    <LastModified>01/02/07 01:09</LastModified>
    <Created>31/01/07 22:42</Created>
</Header>
<Body>
    <Notes><![CDATA[]]></Notes>
    <Text><![CDATA[This is the body of the message that needs to be copied]]></Text>
</Body>
</Story>"

tell application "System Events"
	set xmlData to make new XML data with properties {name:"xmldata", text:xmlText}
	tell XML element "Story" of xmlData
		set theTitle to value of XML element "Title" of XML element "Header"
		set theCreated to value of XML element "Created" of XML element "Header"
		set theText to value of XML element "Text" of XML element "Body"
	end tell
end tell

return {theTitle, theCreated, theText}

If you are trying to get this information from a file then use this line instead…

set xmlData to contents of XML file "path:to:the:file.xml"

Hi Regulus

Thanks for the script I tried using the following

set xmlText to contents of "/Users/jasondoyle/Desktop/1163498795187/story_1170293659953.xml"
tell application "System Events"
	set xmlData to make new XML data with properties {name:"xmldata", text:xmlText}
	tell XML element "Story" of xmlData
		set theTitle to value of XML element "Title" of XML element "Header"
		set theCreated to value of XML element "Created" of XML element "Header"
		set theText to value of XML element "Text" of XML element "Body"
	end tell
end tell

return {theTitle, theCreated, theText}

But got the following error. If you have any pointers I would be very grateful

Applescript doesn’t use slash delimited paths, it uses colon delimited paths. Plus you need to get the “contents” of the file inside the system events block of code as I showed…

set xmlPath to "/Users/jasondoyle/Desktop/1163498795187/story_1170293659953.xml"
set xmlMacPath to (POSIX file xmlPath) as text

tell application "System Events"
	set xmlData to contents of XML file xmlMacPath
	tell XML element "Story" of xmlData
		set theTitle to value of XML element "Title" of XML element "Header"
		set theCreated to value of XML element "Created" of XML element "Header"
		set theText to value of XML element "Text" of XML element "Body"
	end tell
end tell

return {theTitle, theCreated, theText}

Thanks again Regulus

I didn’t tinker at all with the script you last posted and I appreciate the guidance but unfortunately when I ran the script I got the following error:

It highlighted the word contents as being the falling down point.

set xmlPath to "/Users/jasondoyle/Desktop/story.xml"
set xmlMacPath to (POSIX file xmlPath) as text

tell application "System Events"
	set xmlData to contents of XML file xmlMacPath
	tell XML element "Story" of xmlData
		set theTitle to value of XML element "Title" of XML element "Header"
		set theCreated to value of XML element "Created" of XML element "Header"
		set theText to value of XML element "Text" of XML element "Body"
	end tell
end tell

return {theTitle, theCreated, theText}

Hank’s script runs fine here. Contents being highlighted suggests there’s a problem with the data file. Is there a typo in the path string? Does the file have an xml extension? Is it plain text?
(System Events seems to not care about line endings - both and work).