Re: More XML stuff - reading - changing - and writing the file
Re: More XML stuff - reading - changing - and writing the file
- Subject: Re: More XML stuff - reading - changing - and writing the file
- From: Emmanuel LEVY <email@hidden>
- Date: Thu, 10 Jun 2010 01:50:40 +0200
Sorry I'm late with my mails, but maybe you are interested in a few
minor comments.
try
close access POSIX file tempXML
end try
don't do that, xmllib will do all the file stuff for you
-- 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))
don't do that, you're assuming you know the file's encoding, it's
safer to let xmllib assume for you :
set xmltext to xmlopen tempxml
is enough. I assume tempxml is a posix path: "/User/steve/blah/
thefile.xml"
-- 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
The idea in XML is that nodes have tag names.
Let's assume you know the tag name of the_child, say <father>.
Let's assume you know the tag name of the_child2, say <sonny>.
Let's assume you know the name of the first attribute of <sonny>, it's
"type". Then:
the "the_child2" you're wanting is:
XMLXPath the_root with " father[ 1 ] / sonny [ @type = 'ASSET_TYPE' ] "
This reads from left to right: take all the direct children of the
root whose tag name is father. Take the first of them. Take all its
"sonny" children. Select the one(s) whose attribute with name "type"
has value "ASSET_TYPE".
Thus the block above can be:
XMLRemove XMLXPath the_root with " father[ 1 ] / sonny [ @type =
'ASSET_TYPE' ] "
or, if you want to save AppleEvents:
XMLRemove {XMLRef:the_root, xpath pattern:" father[ 1 ] / sonny
[ @type = 'ASSET_TYPE' ] "}
-- 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
You don't need that. Just say:
XMLSave the_root
XMLLib knows what file we're talking about. You forgot a minor thing:
clean-up with XMLClose the_root. Which gives:
-- untested, sorry...
set the_root to XMLRoot (XMLOpen tempxml)
XMLRemove {XMLRef:the_root, xpath pattern:" father[ 1 ] / sonny
[ @type = 'ASSET_TYPE' ] "}
XMLSave the_root
XMLClose the_root
Emmanuel
_______________________________________________
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