• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: More XML stuff - reading - changing - and writing the file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: More XML stuff - reading - changing - and writing the file


  • Subject: Re: More XML stuff - reading - changing - and writing the file
  • From: Steve Thompson <email@hidden>
  • Date: Wed, 09 Jun 2010 15:18:14 -0700

Hi all.

Sometimes you gotta write about it, walk away, and look at it again to figure out the error of your ways. I think I have it working now. I;m sure the code could be consolidated.

Basically I was confusing when to use my path value defined as a string in a "do shell script" call and when to tell the Mac that it was a POSIX path specifically. Thus my open with access was getting screwy.

Secondly I had to work out exacty what I was doing with XMLRemove and XMLDisplayXML to get back the text file that I wanted to write back to the file on HDD.

It seems to be working now. Here is my working script snippet:

try
close access POSIX file tempXML
end try


-- open XMLfile and prep as XML
set xmlFileText to (open for access POSIX file tempXML with write permission)
set XMLtext to XMLOpen (read xmlFileText for (get eof xmlFileText))

-- find the index of the XML entry of interest
set the_root to XMLRoot of XMLtext
set the_child to XMLChild the_root index 1
XMLNodeInfo the_child
set theCount to the XMLCount of the_child

repeat with i from 1 to theCount
set the_child2 to XMLChild the_child index i
set theValueID to item 2 of attribute of (XMLNodeInfo the_child2)
if theValueID is "ASSET_TYPE" then set theENTRY to i
end repeat

-- once the index is found, remove that entry
set theOne to XMLChild the_child index theENTRY
XMLRemove theOne

-- try to write the resulting XML text back to the file
set eof of POSIX file tempXML to 0
set xmlFileText to XMLDisplayXML the_root
-- display dialog xmlFileText
write xmlFileText to POSIX file tempXML

-- close file on HDD
close access tempXML


Steve Thompson
email@hidden




On Jun 9, 2010, at 2:20 PM, Steve Thompson wrote:

Sorry, correction I inadvertently pasted with " as Unicode text" at the end of my XMLOpen line. Pretend it is not there.

(Script as it is (and still with problems) below.

Steve Thompson
email@hidden
310.359.0323




On Jun 9, 2010, at 10:41 AM, Steve Thompson wrote:

Hey guys, 

Thanks again for all the great help. I am using XMLlib and have been very successful in extracting values of interest from my XML generated from Final Cut server.

Now I am having some issues in that my goal is to read in an XML file, remove an entry, and write that back out to file for later processing.  I can read in the XML, parse it and remove the entry of interest successfully (using XMLDisplayXML to see my results) but I am having issues getting that changed data back to disk. 

1.  It is unclear to me whether I can use XMLLib to directly affect the file on disk or if I have to (as I am here) read in the file as text to a variable and then process that using XMLOpen...etc and then put that XML data back into text and write that new text file out to disk.

2.  I haven't had this issue before, but my "open for access" works just fine to read in the file, but as soon as I add "with write permission" I get a -49 error that the file is already open (thus I added  "try to close the file" first and still get the error). Anyone want to comment about this as I think I've been staring at it too long and can't see the error of my ways.

Again, thanks again, you guys have been great and thanks in advance for your comments on this one.

Cheers,

Steve

Here is the portion of code of interest:

-- path to XML file to be modified
set the tempXML to "/Volumes/BCP_XSAN/fcsvr/TEMP/tempxmltest.txt"

-- ask Final Cut Server to generate the XML file
do shell script "\"" & fcsCommand & "\"" & " getmd --xml /asset/" & assetID & " > " & tempXML

-- I keep getting "file already open (-49) errors, so I tried to make sure it was closed - didn't help
try
close access tempXML
end try


-- open XMLfile and prep as XML
set xmlFileText to (open for access tempXML with write permission) -- this is where I get -49
set XMLtext to XMLOpen (read xmlFileText for (get eof xmlFileText))

-- find the index of the XML entry of interest
set the_root to XMLRoot of XMLtext
set the_child to XMLChild the_root index 1
XMLNodeInfo the_child
set theCount to the XMLCount of the_child

repeat with i from 1 to theCount
set the_child2 to XMLChild the_child index i
set theValueID to item 2 of attribute of (XMLNodeInfo the_child2)
if theValueID is "ASSET_TYPE" then set theENTRY to i
end repeat

-- once the index is found, remove that entry
set theOne to XMLChild the_child index theENTRY
XMLDisplayXML theOne
XMLRemove theOne

-- try to write the resulting XML text back to the file
set xmlFileText to XMLGetText the_root
write xmlFileText to tempXML

-- close file on HDD
close access tempXML





Steve Thompson
email@hidden




Do not post admin requests to the list. They will be ignored. AppleScript-Users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: Archives: http://lists.apple.com/archives/applescript-users This email sent to email@hidden
  • Follow-Ups:
    • Re: More XML stuff - reading - changing - and writing the file
      • From: Emmanuel LEVY <email@hidden>
References: 
 >More XML stuff - reading - changing - and writing the file (From: Steve Thompson <email@hidden>)
 >Re: More XML stuff - reading - changing - and writing the file (From: Steve Thompson <email@hidden>)

  • Prev by Date: Re: OS-9 question on try with on error
  • Next by Date: Re: Filemaker and Database events
  • Previous by thread: Re: More XML stuff - reading - changing - and writing the file
  • Next by thread: Re: More XML stuff - reading - changing - and writing the file
  • Index(es):
    • Date
    • Thread