Re: AS records & LNS XML Tools
Re: AS records & LNS XML Tools
- Subject: Re: AS records & LNS XML Tools
- From: has <email@hidden>
- Date: Tue, 20 Aug 2002 02:14:12 +0100
Phi Sanders wrote:
>
Can someone point me in the right direction please? I need to parse some
>
XML (using the free LNS XML tools) and I've written everything around
>
recursion but can't get my test to work.
[..]
>
if iElement contains |XML contents| then
Won't work. Records don't behave this way. You need to concatenate another
record onto iElement:
iElement & {XML contents:missing value}
and then test iElement's XML contents to see if it's a list (iElement has
content of some sort) or is missing value (iElement's a leaf node without
content). If you find a list, test that list to see if it contains a single
string (iElement's a leaf node containing content), or one or more records
(iElement contains other nodes, so keep on drilling).
Note: I'm sure we had the exact same question asked here about a week ago.
Perhaps you should try looking through the archives for that thread?
BTW, for anyone who cares, I threw together the following incredibly crude
and totally design-free library that dumps the XML string into a basic DOM
for you to play with:
======================================================================
property AALib : load script (file "frank:libraries:associativearrayli
[NO-BREAK]bfolder:associativearraylib")--your path here
--
on _addSubElementsToAssocArray(contentsList)
set contentsArray to AALib's newAssociative({valueIfNotFound:missin
[NO-BREAK]g value})
script --speed kludge
property _contents : contentsList
end script
tell result
repeat with n from 1 to count contentsList
set subElement to its _contents's item n
set xmlContent to _wrapThisElement(subElement)
contentsArray's appendValue(subElement's XML tag, xmlContent)
end repeat
end tell
return contentsArray
end _addSubElementsToAssocArray
on _newNode(xmlTag, xmlAttributes, elementContent, objectHasContent,
[NO-BREAK]objectHasElements)
script node
property _tag : xmlTag
property _attributes : xmlAttributes
property _contents : elementContent
property _hasContent : objectHasContent
property _hasElements : objectHasElements
--
on hasElements()
return _hasElements
end hasElements
on hasContent()
return _hasContent
end hasContent
--
on allElementNames()
return _contents's listKeys()
end allElementNames
on numberOfElementsNamed(tagName)
return count of _contents's getValue(tagName)
end numberOfElementsNamed
--
on theElement(tagName, tagNumber)
return _contents's getValue(tagName)'s item tagNumber
end theElement
on theAttributes()
return _attributes
end theAttributes
on theContent()
return _contents
end theContent
end script
return node
end _newNode
on _wrapThisElement(thisElement)
set elementAttributes to XML attributes of (thisElement & {XML
[NO-BREAK]attributes:{}})
set elementContent to XML contents of (thisElement & {XML
[NO-BREAK]contents:missing value})
if elementContent is missing value then --empty leaf node
set objectHasContent to false
set objectHasElements to false
else if ((count elementContent) is 1) and (elementContent's first
[NO-BREAK]item's class is string) then --non-empty leaf node
set elementContent to thisElement's first item
set objectHasContent to true
set objectHasElements to false
else --non-empty node
set elementContent to _addSubElementsToAssocArray(elementContent
[NO-BREAK])
set objectHasContent to true
set objectHasElements to true
end if
_newNode(thisElement's XML tag, elementAttributes, elementContent,
[NO-BREAK]objectHasContent, objectHasElements)
end _wrapThisElement
--
on wrapXML(xmlString) --main call
return _wrapThisElement(parse XML xmlString)
end wrapXML
-------
--TEST
set theXML to "<library>
<topic title=\"Books by Topic\">
<subject title=\"Autobiography\">
<item vid=\"4-9-813-1970\" title=\"Alexander - My life as
[NO-BREAK]a Geek\"/>
</subject>
<divider />
<subject title=\"Fantasy\">
<item vid=\"1-888-444-3845\" title=\"Mephisto - What is
[NO-BREAK]Evil?\"/>
</subject>
</topic>
</library>"
set xmlOM to wrapXML(theXML)
log allElementNames() of xmlOM
--numberOfElementsNamed("topic") of xmlOM
set topicElements to theElement("topic", 1) of xmlOM
log allElementNames() of topicElements
log
repeat with n from 1 to (numberOfElementsNamed("subject") of
[NO-BREAK]topicElements)
log theAttributes() of theElement("item", 1) of
[NO-BREAK]theElement("subject", n) of topicElements
end repeat
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
Very, very basic. Node/element objects contain only the bare minimum of
methods required to allow navigation (much like getting around the Finder,
except the syntax is more clinical). I've no plans for doing anything with
it, but folks are free to develop the idea further if they wish. [1]
Uses XML Tools, along with the associativeArrayLib on my site
[
http://www.barple.connectfree.co.uk].
has
[1] I have considered writing a proper wrapper library for XML Tools in the
past; one that'll allow writing as well as reading [this one only does the
latter] and supports all the XML bells and whistles. But if I were to do so
I'd be at least a week on it [XML becomes pretty darned complicated once
you get into all the extra stuff], and that's time I can much better spend
on other things (short of being paid to do it, of course;).
--
(My email address has changed from <email@hidden> to
<email@hidden>. Please update your address books accordingly.)
_______________________________________________
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.