Re: parsing XML
Re: parsing XML
- Subject: Re: parsing XML
- From: has <email@hidden>
- Date: Sat, 9 Mar 2002 00:15:37 +0000
Guillaume Iacino wrote:
[XML parsing/editing via XML Tools]
>
Any insights, tips, examples or links will more very appreciated.
If all you're doing is generating very simple XML files like the example
shown, there's easier ways than going via XML Tools. If, however, you're
working with existing XML files then it's probably safest.
Here's some - frankly pretty awful - code I dug up on my HD. Must've
written it for someone else a while back; don't really remember who or why
now, but no matter.
It was written for navigating around the rather convoluted records+lists
structures that XML Tools has to generate (due to AS <ahem> lacking certain
features that'd make this stuff about a billion times simpler:p). Dunno if
it'll really help - it'll certainly need some modification to do what you
need - but it might give you a bit of a boost towards a solution.
======================================================================
--THE HANDLERS
on getXMLByHierarchy(theXML, theChain)
set thePath to {}
try
if theChain's length is greater than 1 then
repeat with eachItem in items 1 thru -2 of theChain
set thePath's end to eachItem as list
set theXML to matchedItems of listMatchingXMLTags(theXML,
[NO-BREAK]eachItem's item 1)
if theXML is {} then error generateNotFoundError(tag,
[NO-BREAK]eachItem, thePath)
set theXML to XML contents of theXML's item (eachItem's
[NO-BREAK]item 2)
end repeat
end if
item (theChain's last item's item 2) of matchedItems of
[NO-BREAK]listMatchingXMLTags(theXML, theChain's last item's item 1)
on error number -1728
error {notFound:theChain, didFind:thePath} number 500
end try
end getXMLByHierarchy
on listMatchingXMLTags(listOfRecords, theLabel)
if listOfRecords's class is not list then set listOfRecords to
[NO-BREAK]{listOfRecords}
set theRecs to findTagsInList(listOfRecords, theLabel)
getTagsByListOffsets(listOfRecords, theRecs's matchedItems)
{matchedItems:result, unmatchedItems:theRecs's unmatchedItems}
end listMatchingXMLTags
on findTagsInList(listOfRecords, theTag)
set matchedItems to {}
set unmatchedItems to {}
considering case
repeat with x from 1 to count listOfRecords
if listOfRecords's item x's XML tag is theTag then
set matchedItems's end to x
else
set unmatchedItems's end to x
end if
end repeat
end considering
{matchedItems:matchedItems, unmatchedItems:unmatchedItems}
end findTagsInList
on getTagsByListOffsets(listOfRecords, theOffsets)
set theList to {}
repeat with x in theOffsets as list
set theList's end to listOfRecords's item x
end repeat
theList
end getTagsByListOffsets
------------------------------
--and here's some test code
set theXML to "<Painting>
<Color>Red</Color>
<Color>Yellow</Color>
<Color>White</Color>
</Painting>
"
set XMLRec to parse XML theXML
(*
theChain is a list of lists, where each sublist is of form {elementName,
elementNumber}. So if you want the record for the second <Color>, you'd use
the following:
*)
set theChain to {{"Painting", 1}, {"Color", 2}}
getXMLByHierarchy(XMLRec, theChain)
--> {class:XML element, XML tag:"Color", XML contents:{"Yellow"}}
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
(Alternatively, you could just use a language that has better XML support
built in.:)
HTH
has
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.