Parsing XML into sorted elements

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Parsing XML into sorted elements

Sean P. DeNigris
Administrator
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
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Parsing XML into sorted elements

Sean P. DeNigris
Administrator
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
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Parsing XML into sorted elements

NorbertHartl

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


Reply | Threaded
Open this post in threaded view
|

Re: Parsing XML into sorted elements

patmaddox
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

Reply | Threaded
Open this post in threaded view
|

Re: Parsing XML into sorted elements

Sean P. DeNigris
Administrator
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.
Cheers,
Sean