Login  Register

How to access XML tag name?

Posted by stepharo on Mar 11, 2016; 12:37pm
URL: https://forum.world.st/How-to-access-XML-tag-name-tp4884065.html

Hi

Yesterday I started to hack a smart SAX handler. The idea is that I want
to just specify the tags
I want to visit and the SAX handler should invoke (generated) visit
methods. Like that I can easily
get visitors on XML domain.

Here is an example of what I did.

     | h |
     h := SmartSAXHandler new
                 visitor: (MyFilmVisitor new visitTags: #(FILM ROLE));
                 on: FileSystem workingDirectory / 'FILMS.XML'.
     h parseDocument.
     ^ h



SmartSAXHAndler >> startElement: aQualifiedName attributes: aDictionary

     (visitor shouldVisit: aQualifiedName)
         ifTrue: [
                 visitor
                     perform: (visitor createdVisitSelector: aQualifiedName)
                     with: aQualifiedName
                     with: aDictionary
         ]


Object subclass: #GenericTagSAXVisitor
     instanceVariableNames: 'visitTag visitTags'
     classVariableNames: ''
     category: 'SmartXMLHandler'


GenericTagSAXVisitor>>visitTags: aCollection
     "set the tags that will lead to a call to a visitTag:with: method
in the visitor"

     visitTags := aCollection collect: [ :each | each asLowercase ].
     self createVisitMethods.

and in a subclass the visit* methods automatically generated


Now I could not get when I have a <TITRE>Vertigo</TITRE> where I can get
the Vertigo information.
I redefined several methods of SAXhandler but without success.


<?xml version="1.0" encoding="iso-8859-1"?>
<FILMS>
   <FILM annee="1958">
     <TITRE>Vertigo</TITRE>
     <GENRE>Drame</GENRE>
     <PAYS>USA</PAYS>
     <MES idref="3"/>
     <ROLES>
       <ROLE>
         <PRENOM>James</PRENOM>
         <NOM>Stewart</NOM>
         <INTITULE>John Ferguson</INTITULE>
       </ROLE>
       <ROLE>
         <PRENOM>Kim</PRENOM>
         <NOM>Novak</NOM>
         <INTITULE>Madeleine Elster</INTITULE>
       </ROLE>
     </ROLES>
     <RESUME>Scottie Ferguson, ancien inspecteur de police, est sujet
au vertige depuis qu'il a vu mourir son
  collegue. Elster, son ami, le charge de surveiller sa femme,
Madeleine, ayant des tendances
  suicidaires. Amoureux de la jeune femme Scottie ne remarque pas le
piege qui se trame autour
  de lui et dont il va etre la victime... </RESUME>
   </FILM>