--PRIVATE PROPERTIES property _asXML property _pDoneParsing property _isParser property _pCallBackHandler property _pCallBackObject --PUBLIC PROPERTIES (Parsed Properties) -- (these are node properties) -- (the same props used by xtra("XMLParser")) property type property text property name property child --[:] property attributeName --[] property attributeValue --[] property count -- property attribute --[:] --CONSTRUCTION METHODS on new (me, node, xmlSTR) _pDoneParsing = false _isParser = false me.init(node, xmlSTR) return me end on init (me, node, xmlSTR) _asXML = void if voidP(node) then --init xml document object --new ActionScript object --clearAsObjects() if stringP(xmlSTR) then _asXML = newObject("XML", xmlSTR) else _asXML = newObject("XML") end if --default is true in XML Parser Xtra me.ignoreWhitespace(true) --init temp child prop for term child = void child = [:] attribute = void attribute = [:] count = 0 if stringP(xmlSTR) then me._initParsedProperties() end if else --init xml node object _asXML = node me._initParsedProperties() end if end on term (me) call(#term, child) child = void attributeName = void attributeValue = void attribute = void _asXML = void end --PUBLIC METHODS (based on XML Parser Xtra Interface) on parseURL(me, rURL, rCallBackHandler, rCallBackObject) _pDoneParsing = false _pCallBackHandler = rCallBackHandler _pCallBackObject = rCallBackObject _asXML.setCallback("onLoad", #_XMLloaded, me)--ancestor _asXML.load(rURL)--ancestor end on doneParsing(me) return _pDoneParsing end -- on makeList(me) tList = me._toList() --["ROOT OF XML DOCUMENT":["!ATTRIBUTES": [:],\ "e1": ["!ATTRIBUTES": [:],\ "tagName": ["!ATTRIBUTES": ["attr1": "val1", "attr2": "val2"]],\ "e2": ["!ATTRIBUTES": [:], "!CHARDATA": "element 2"],\ "e3": ["!ATTRIBUTES": [:], "!CHARDATA": "element 3"]]]] return tList end on makeSublist(me) tList = me._toList() ---- ["tagName": ["!ATTRIBUTES": ["attr1": "val1", "attr2": "val2"]]] return tList end on ignoreWhitespace(me, trueOrFalse) _asXML.ignoreWhite = trueOrFalse --ancestor end on parseString(me, rString) _asXML.parseXML(rString) --ancestor err = me.getError() if voidP(err) then me._initParsedProperties() end if return err end on getError(me) err = _asXML.status--ancestor if err < 0 then return (err && me._getErrorString(err)) end if return --(void) --XML Parser Xtra returns void if there was no error end --PUBLIC METHODS (for ease of use) on getAt (me, rIndexOrString) if integerP(rIndexOrString) then return child[rIndexOrString] else if stringP(rIndexOrString) then return child[rIndexOrString] else --alert("error: integer of string expected") end if end --on getPropRef(me, propRef) on getText (me) --finds the first text node (should always only be one) and returns string if count > 0 then repeat with n = 1 to count if stringP(child[n].text) then return child[n].text end if end repeat end if return EMPTY end --PRIVATE METHODS -- on _toList (me, rParentList) tList = [:] tList.addProp("!ATTRIBUTES", attribute)--attribute.duplicate() call(#_toList, child, tList) if not voidP(rParentList) then if type = #element then tName = name rParentList.addProp(tName, tList) else -- #procInst,#text: tName = "!CHARDATA" tList = text rParentList.addProp(tName, tList) end if else tName = name end if --return list rList = [:] rList.setaProp(tName, tList) return rList end on _XMLloaded (me) _pDoneParsing = true --callback err = me.getError() if voidP(err) then me._initParsedProperties() end if if not voidP(_pCallBackObject) then call(_pCallBackHandler, _pCallBackObject, err) end if end on _getErrorString (me, err) case err of 0: errString = "No error; parse was completed successfully." -2: errString = "A CDATA section was not properly terminated." -3: errString = "The XML declaration was not properly terminated." -4: errString = "The DOCTYPE declaration was not properly terminated." -5: errString = "A comment was not properly terminated." -6: errString = "An XML element was malformed." -7: errString = "Out of memory." -8: errString = "An attribute value was not properly terminated." -9: errString = "A start-tag was not matched with an end-tag." -10: errString = "An end-tag was encountered without a matching start-tag." end case return errString end on _initParsedProperties (me) nodeType = integer(_asXML.nodeType)--ancestor if not voidP(nodeType) then tList = [#element,#procInst,#text] type = tList[nodeType] tList = void text = _asXML.nodeValue--ancestor name = _asXML.nodeName--ancestor --childNodes child = void child = [:] count = integer( _asXML.childNodes.length )--ancestor repeat with i = 1 to count rType = _asXML.childNodes[i-1].nodeType--ancestor if rType = 1 then rName = _asXML.childNodes[i-1].nodeName--ancestor else rName = i end if child.addProp( rName, script("XmlParser_AS").new( _asXML.childNodes[i-1] ) )--ancestor end repeat --attributes attribute = [:] attributeName = [] attributeValue = [] if voidP(value("_asXML.xmlDecl")) then --parent object has no attributes --ancestor --we need to parse the Attribute Names because the ancestor.attributes object cannot be scanned for property names nodeString = _asXML.toString()--ancestor the itemDelimiter = ">" nodeTag = nodeString.item[1] the itemDelimiter = "=" tLength = nodeTag.item.count-1 if tLength > 0 then repeat with n = 1 to tLength tokenString = nodeTag.item[n] tname = tokenString.word[tokenString.word.count] attributeName.add(tname) tValue = value("_asXML.attributes."& tname)--ancestor if voidP(tValue) then --work around for flashObject attributes tValue = empty end if attributeValue.add(tValue) attribute.addProp(tname, tValue) end repeat end if else --this is the top level parent of root _isParser = true name = "ROOT OF XML DOCUMENT" end if _pDoneParsing = true return i-1 else return -1 --this will trip an error if adding an element end if end ----------------------------------- on addElementNode(me, rName, attr_pl) doc1 = newObject("XML", "<"&rName&"/>") node = doc1.lastChild.cloneNode(true) doc1 = void _asXML.appendChild(node)--ancestor --optional add attributes proplist if listP(attr_pl) then i = attr_pl.count repeat while i --node.setAttribute(attr_pl.getPropAt(i), attr_pl.getAt(i)) do ("node.attributes."& attr_pl.getPropAt(i) &&"="&"e& attr_pl.getAt(i) "e)--ancestor i=i-1 end repeat end if -- i = me._initParsedProperties() return me[i]--need a direct path to node ** end on setXmlDecl (me, rXmlDeclStr) if voidP(rXmlDeclStr) then rXmlDeclStr = "" _asXML.xmlDecl = rXmlDeclStr--ancestor return rXmlDeclStr end on setAttribute(me, rAtt, rVal) do ("_asXML.attributes."& rAtt &&"="&"e& rVal "e)--ancestor me._initParsedProperties() end ----------------------------------- --AS XML Object methods and properties: --me.load(url) --me.loaded --me.xmlDecl --me.hasChildNodes() --me.appendChild() --me.toString() on toString (me) return _asXML.toString() --ancestor end --me.attributes --me.parentNode --me.childNodes --me.firstChild --me.lastChild --me.nextSibling --me.previousSibling --me.nodeName --me.nodeType (1=element 3=text) --me.nodeValue