Hi all..
So, I've got an XML file that looks like the following : <?xml version="1.0"?> <!DOCTYPE requirements [ <!ELEMENT requirements (UseCase+)> <!ELEMENT UseCase (#PCDATA)> <!ELEMENT UseCases (UseCase+)> ]> <requirements> <UseCases> <UseCase>Use-Case #1</UseCase> <UseCase>Use-Case #2</UseCase> </UseCases> </requirements> I then have the following code to parse & read in the XML : ============================================================== buildUseCaseCollectionFrom: aList [ aList detect: [:each | Transcript show: 'Element:' , (each characterData); cr ] ] stream := './requirements.xml' asFile readStream. parser := XML.XMLParser new. parser validate: false. parser parse: stream. docroot := parser document root. self buildUseCaseCollectionFrom: (docroot elementsNamed: 'UseCases'). ============================================================== When I run it, I get the following output -- seems like the Collection of Use-Case Elements is not quite right.. I was expecting two "Element:" lines each with the respective Use-Case # element but not both bunched up as a single.. Any ideas on what I've messed up? Element: Use-Case #1 Use-Case #2 Object: TextCollector new "<-0x53b6698>" error: Invalid argument Transcript: must be a Boolean SystemExceptions.MustBeBoolean(Exception)>>signal (ExcHandling.st:254) SystemExceptions.MustBeBoolean class(SystemExceptions.WrongClass class)>>signalOn:mustBe: (SysExcept.st:805) SystemExceptions.MustBeBoolean class>>signalOn: (SysExcept.st:881) TextCollector(Object)>>mustBeBoolean (Object.st:1399) [] in Array(Iterable)>>detect:ifNone: (Iterable.st:107) Array(SequenceableCollection)>>do: (SeqCollect.st:812) Array(Iterable)>>detect:ifNone: (Iterable.st:108) Array(Iterable)>>detect: (Iterable.st:172) _______________________________________________ help-smalltalk mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-smalltalk |
Il 25/09/2012 18:51, Rick Flower ha scritto:
> buildUseCaseCollectionFrom: aList [ > aList detect: [:each | Transcript show: 'Element:' , (each > characterData); cr ] > ] > > stream := './requirements.xml' asFile readStream. > parser := XML.XMLParser new. > parser validate: false. > parser parse: stream. > > docroot := parser document root. > self buildUseCaseCollectionFrom: (docroot elementsNamed: 'UseCases'). > > ============================================================== > > When I run it, I get the following output -- seems > like the Collection of Use-Case Elements is not > quite right.. I was expecting two "Element:" lines > each with the respective Use-Case # element but not > both bunched up as a single.. Any ideas on what I've > messed up? > > Element: > Use-Case #1 > Use-Case #2 #detect: is not doing what you want. It returns the first element for which the block returns true. Note that your block does not return a boolean in the first place. I think this is returning all the character-data inside the <UseCases> element, discarding the elements. Paolo _______________________________________________ help-smalltalk mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-smalltalk |
Thanks Paolo.. My Smalltalk Collections memory is apparently dead..
I'll go back and read-up on the collections again.. Been spending way too much time with C++ lately and my smalltalk memory is fading!! If I run into another snag I'll reply.. I did notice after posting my code snippet that the XML was wrong -- the first Elements line should look like : <!ELEMENT requirements (UseCases+)> and not <!ELEMENT requirements (UseCase+)> _______________________________________________ help-smalltalk mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-smalltalk |
Ok..I changed the method shown earlier to the following
and know that it ought to put each element on its own line : buildUseCaseCollection2From: aList [ aList do: [:each | Transcript show: 'Element:' , (each characterData); cr ] ] ========================== Element: Use-Case #1 Use-Case #2 ========================== Notice the extra line wraps -- one at the beginning and one at the end. It seems like my collection is not being properly setup but I followed the example in the docs which says that if I do a : parser document root elementsNamed: 'UseCases' I ought to get an OrderedCollection with 2 items in it (in this case).. But I seem to have only 1 item with both combined.. Here's the updated XML being parsed : <?xml version="1.0"?> <!DOCTYPE requirements [ <!ELEMENT requirements (UseCases+)> <!ELEMENT UseCase (#PCDATA)> <!ELEMENT UseCases (UseCase+)> ]> <requirements> <UseCases> <UseCase>Use-Case #1</UseCase> <UseCase>Use-Case #2</UseCase> </UseCases> </requirements> Any ideas on what I'm not understanding? Thanks!! --Rick _______________________________________________ help-smalltalk mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-smalltalk |
Got it to parse properly now.. Had two issues :
1) hard ^M (carriage returns) at the end of each XML line -- thanks Eclipse! -- didn't cause any problems just annoying 2) Got my array of use-case elements using the following code: (parser document root elementNamed: 'UseCases') elements reject: [:each | each isBlankText] For whatever reason the array for the returned element has empty entries in it -- and looks like the following in my case : An instance of Array contents: [ [1]: [2]: <UseCase>Use-Case #1</UseCase> [3]: [4]: <UseCase>Use-Case #2</UseCase> [5]: ] After running the above reject: code it becomes : An instance of Array contents: [ [1]: <UseCase>Use-Case #1</UseCase> [2]: <UseCase>Use-Case #2</UseCase> ] YMMV! _______________________________________________ help-smalltalk mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-smalltalk |
Il 25/09/2012 20:29, Rick Flower ha scritto:
> > > 2) Got my array of use-case elements using the following code: > > (parser document root elementNamed: 'UseCases') elements reject: [:each > | each isBlankText] > > For whatever reason the array for the returned element has > empty entries in it -- and looks like the following in my > case : They are not empty, they correspond to the whitespace between <UseCases> and <UseCase>. If you want to ignore it, you need to create a subclass of DOM_SAXDriver and override the #ignorableWhitespace: method. The overridden method can simply do nothing. Then to use your driver call this before starting the parse: parser saxDriver: NoWhitespaceSAXDriver new Paolo > > An instance of Array > contents: [ > [1]: > > [2]: <UseCase>Use-Case #1</UseCase> > [3]: > > [4]: <UseCase>Use-Case #2</UseCase> > [5]: > > ] > > After running the above reject: code it becomes : > An instance of Array > contents: [ > [1]: <UseCase>Use-Case #1</UseCase> > [2]: <UseCase>Use-Case #2</UseCase> > ] _______________________________________________ help-smalltalk mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-smalltalk |
On 26.09.2012 00:08, Paolo Bonzini wrote:
> They are not empty, they correspond to the whitespace between > and. > > If you want to ignore it, you need to create a subclass of > DOM_SAXDriver > and override the #ignorableWhitespace: method. The overridden method > can simply do nothing. Then to use your driver call this before > starting the parse: > > parser saxDriver: NoWhitespaceSAXDriver new Cool! Thanks Paolo!! _______________________________________________ help-smalltalk mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-smalltalk |
Free forum by Nabble | Edit this page |