Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
YAXO and XML
Last updated at 5:09 pm UTC on 21 October 2006
stéphane ducasse February 02, 2005 I'm trying to understand how to use Yaxo and I cannot figure out what is the difference between the SAXDriver and SAXHandler. ... Does anybody has an example?

More on YAX: http://squeaklet.com/Yax/index.htm

David Shaffer SAX is an event drive API for processing XML documents. While java-based it is relatively standard across languages. There are several Java resources but here's one which helped me: http://www.saxproject.org/

Most implementations of SAX (including the one included with the "full" 3.7 image) are mostly "2.0" compliant which seems to me including XML namespaces (someone correct me if I'm wrong here) so the sample code on these pages which specifically deals with namespaces also transfers well to Smalltalk. As for Smalltalk examples, well, look at the subclasses of SAXHandler...one of these uses SAX to build the somewhat standard DOM tree. So, one has two choices when dealing with XML: event drive with SAX (usually, but not always, building the tree yourself) or DOM where the XML tree gets build from generic components (XMLElements).

Bert Freudenberg For quick results I just use the XMLDOMParser. Open an explorer on this:
XMLDOMParser addressBookXMLWithDTD
Then you can traverse the resulting XMLDocument tree and extract whatever you need.

How to access the Desktop and Network Paths in Windows Where does one download YAXO from? http://kilana.unibe.ch:8888/XMLSupport/XML-Parser-mir.2.mcz


How To Use DOM XML

NOTE: I'm not sure who wrote the orginal example. I found it on the coweb.
http://coweb.cc.gatech.edu/cs2340/2862

You have got 2 flavor's of xml parsers. One is SAX and the other is DOM. SAX parses an xml file line by line and it's more dynamic where as DOM reads the entire file and build a tree out of it.

Below you will find snippets of code on how to use DOM XML.

Writing to a file called MyXml.xml

"Code below describes opening a stream and writer and associating the writer with XMLDocument"

doc := XMLDocument new.
stream := FileStream fileNamed: 'MyXml.xml'. "speicifying the stream"
writer := XMLWriter on:stream. "specifying the writer"
writer initialize.

"Code below describes how you could write an xml element with a tag "
elmt := XMLElement named: 'User' attributes: Dictionary new.

"adding the attribute tags and values to the "
childElmt := XMLElement named: 'Name' attributes: Dictionary new.
childElmt addContent: (XMLStringNode string: ('rao')). "adding a tag called Name with the value 'rao'"
elmt addElement: childElmt.

doc addElement: elmt.
doc printXMLOn: writer.
stream close.

Reading from MyXml.xml
f := FileStream fileNamed: 'xml.xml'. "opening a stream"
doc := XMLDOMParser parseDocumentFrom: f.
f close.
xmlUser := (doc elements) at: 1. "now you have the entire user data"

"readng the name"
elmt := xmlUser firstTagNamed: #Name.
userName := elmt contentString.

Notice how once you call the parseDocumentFrom: you don't need to have the stream open because XMLDOMParser builds tree right away from the given stream.