View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Code to Read In an XML File

fileXMLIn: aFileName
"Reads the instructions, including the name and description, in from the
specified xml file as strings with one information item (e.g. instruction)
per line. If the file is not valid it prints an error to the transcript and
returns nil."
| dFileExists aFile xmlDoc textColl actionColl checkActionColl text action checkAction |
textColl _ OrderedCollection new.
actionColl _ OrderedCollection new.
checkActionColl _ OrderedCollection new.
"Checking if the file exists."
dFileExists _ FileDirectory default.
(dFileExists fileExists: aFileName)
ifFalse: ["If it doesn't there is an error."
Transcript show: 'The file: ' , aFileName , ' does not exists.';
cr.
^ nil].
aFile _ FileDirectory default readOnlyFileNamed: aFileName.
xmlDoc _ XMLDOMParser parseDocumentFrom: aFile.
xmlDoc
tagsNamed: #name
contentsDo: [:value | name _ value asString].
xmlDoc
tagsNamed: #descrip
contentsDo: [:value | descr _ value asString].
xmlDoc
tagsNamed: #text
contentsDo: [:value | value asString isNil
ifTrue: [textColl add: '']
ifFalse: [textColl add: value asString]].
xmlDoc
tagsNamed: #action
contentsDo: [:value | value asString isNil
ifTrue: [actionColl add: '']
ifFalse: [actionColl add: value asString]].
xmlDoc
tagsNamed: #checkAction
contentsDo: [:value | value asString isNil
ifTrue: [checkActionColl add: '']
ifFalse: [checkActionColl add: value asString]].
1
to: textColl size
do: [:index |
index > textColl size
ifTrue: [text _ '']
ifFalse: [text _ textColl at: index].
index > actionColl size
ifTrue: [action _ '']
ifFalse: [action _ actionColl at: index].
index > checkActionColl size
ifTrue: [checkAction _ '']
ifFalse: [checkAction _ checkActionColl at: index].
self
addStep: text
action: action
checkAction: checkAction].
aFile close

Link to this Page