Parsing XML into sorted elements
I want to sort nodes of a certain parent node as they are parsed by XMLDOMParser. The following is what I tried so far. It causes the image to hang and CPU to sweat. I've put half a day in - before I continue, what's the best way to do this? Am I on the right path? It seems very complicated ( I almost feel like I'm working with Morphic ;-) ) The parent node is XMLPageElement which is a subclass of XMLElement, in which I overrode #nodesClass to return XMLSortedList. XMLSortedList is a subclass of XMLNodeList with the following overrides: initialize self nodes sortBlock: self sortBlock. sortBlock ^ [ :a :b | a < b ]. species ^ super XMLSortedList. There is also XMLSortedList class>>collectionClass ^ SortedCollection. The above overrides XMLOrderedList's use of OrderedCollection. I refactored XMLOrderedList by pulling all references to OrderedCollection into this method. Thanks. Sean -- View this message in context: http://forum.world.st/Parsing-XML-into-sorted-elements-tp3615867p3615867.htm... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
I did the simplest thing I could think of and overrode XMLPageElement>>handleEndTag ^ self nodes sort: [ :a :b | a < b ]. It worked but feels like a hack. Anyone have anything better? - S -- View this message in context: http://forum.world.st/Parsing-XML-into-sorted-elements-tp3615867p3615899.htm... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
Am 22.06.2011 um 05:11 schrieb Sean P. DeNigris:
I did the simplest thing I could think of and overrode XMLPageElement>>handleEndTag ^ self nodes sort: [ :a :b | a < b ].
It worked but feels like a hack. Anyone have anything better?
Why do need to sort while the parser is parsing? Can't you sort just afterwards? I think this is the cleanest approach. If you want to sort the elements immediately than you need custom classes like you wrote. Did you have a look at XMLPluggableElementFactory ? You can set a nodeFactory in the XMLDOMParser that should emit your classes while parsing. In your custom class there should be a possibility to override nodesClass in a clean way to get the list changes done. If not that would be a needed enhanced to the parser. Norbert
On Jun 23, 2011, at 7:14 AM, Norbert Hartl wrote:
Am 22.06.2011 um 05:11 schrieb Sean P. DeNigris:
I did the simplest thing I could think of and overrode XMLPageElement>>handleEndTag ^ self nodes sort: [ :a :b | a < b ].
It worked but feels like a hack. Anyone have anything better?
Why do need to sort while the parser is parsing? Can't you sort just afterwards? I think this is the cleanest approach.
Yeah, my guess is that sorting a collection while it's being iterated upon is bad news. Pat
Pat Maddox-3 wrote:
Yeah, my guess is that sorting a collection while it's being iterated upon is bad news.
I wanted to replace the parser's use of OrderedCollection with SortedCollection, but it got too complicated and I ended up sorting at the end in one override with one line of code. -- View this message in context: http://forum.world.st/Parsing-XML-into-sorted-elements-tp3615867p3621048.htm... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
participants (3)
-
Norbert Hartl -
Pat Maddox -
Sean P. DeNigris