I am trying to recompile a text file that contains xml tags within it.
I know how to read to a file, but I don’t know a good way to search through a file using a start and end tag as the search criteria, and then move/copy that string to either the end of a new file, or the end of the same file.
In other words, I am searching for a method to read through a XML text document and organize the XML tags within so they flow in a consistent structure. I also intend to strip the “” and " tags out from the file.
Unorganized contents of text file below: (subject2, qualifier2, subject1, qualifier1, offer2, offer1
------->>>>
other blah blah blah
other random textother blah blah blahother random text
blah blah blahoffer1
Below is the result I am trying to achieve. Notice how the text file below is organized: (offer1, subject1, qualifier1, offer2, subject2, qualifier2)
-------->>>>
blah blah blahother blah blah blahother random textblah blah blahother blah blah blahother random text
Any help or suggestions would be greatly appreciated.
set theText to "<Story><subject2>other blah blah blah</subject2></Story>
<Story><qualifier2>other random text</qualifier2><subject1>other blah blah blah</subject1></Story><Story><qualifier1>other random text</qualifier1></Story>
<Story><offer2>blah blah blah</offer2></Story><Story>offer1</Story>"
set theVar to text ((offset of "<subject2>" in theText) + 0) thru ((offset of "</subject2>" in theText) + 10) of theText
from there it’s just a matter of placing theVar where you want it. StefanK will tell you to use Text Item Delimiters, which is why he will smack me for showing you this. =)
You will need to edit the +0 and +10 to + or - however long the offset is.
I suppose I spoke to soon and assumed I can read a file. I am not sure why your script doesn’t work when trying to read a .xml file whose contents include the text pasted below?
I also tried repeating this thru the 3 paragraphs in this file but that didn’t help either. The scrip I am using is as follows.
tell application "Finder"
set userFile to ((path to current user folder as text) & "test.xml")
try
set userData to open for access file userFile with write permission
set theText to (read userData)
try
set theVar to text ((offset of "<
tell application “Finder”
set userFile to ((path to current user folder as text) & “test.xml”)
try
set userData to open for access file userFile with write permission
set theText to (read userData)
try
set theVar to text ((offset of x in theText) + 0) thru ((offset of y in theText) + 10) of theText
display dialog theVar as string
end try
close access userData
on error
try
close access file userFile
end try
end try
end tell
I have no idea why this isn’t compiling: But the x in the script above should be “” and the y should be "
I jsut added some error handeling. The only issue that came up for me was x and y not being declared, test.xml loaded just fine… Give this a run and see if you get an error.
property x : "<subject2>"
property y : "</subject2>"
tell application "Finder"
set userFile to ((path to current user folder as text) & "test.xml")
display dialog userFile
try
set userData to open for access file userFile with write permission
set theText to (read userData)
try
set theVar to text ((offset of x in theText) + 0) thru ((offset of y in theText) + 10) of theText
display dialog theVar as string
on error emsg
display dialog emsg
end try
close access userData
on error emsg
display dialog emsg
try
close access file userFile
end try
end try
end tell
It seems like theText isn’t being set. Put
display dialog theText
underneath
set theText to (read userData)
Run it and lemme know. Like I said, this script works perfect on my MacBook Pro. I even copied the xml you posted and created a test.xml