[ANN] XML parser (v0.1)
Gabor Szokoli
gabor.szokoli at vanderbilt.edu
Mon Apr 1 22:57:23 CEST 2002
From: "Denys Duchier" <Denys.Duchier at ps.uni-sb.de>
> Since Gabor just brought up my expat interface, I'll announce a pure
> Oz alternative.
The expat thing is lightning-fast and its external dependency is a
single .dll or .so in runtime, so I'll stick with that.
It may look like a horrible abuse of the delicate XML technology, but
we use it to pass around plain recods of data, and in case anyone else
needs to do only that, here's a little script that might help:
(Should be easy to adopt to the parser package, now it relies on the
parser.oz example of the expat package)
functor
import
Browser
X('class' : Parser) at 'Parser.ozf'
define
%Turns the element into a record.
% The label is the element type.
% The features are the attributes and all contained elemet types.
fun {ParseElement Element}
{Record.adjoin
{Parsealist
{Value.condSelect Element alist nil}
}
{ParseContent Element.content {Value.condSelect Element type
root}}
}
end
% Turns the contents of an element into a record.
% The label is passed at Label.
% The features are the unique element types.
% Under each feature there is a list of elements of said type.
fun {ParseContent Content Label}
D
in
{Dictionary.new D}
for E in Content do
%Only elements are considered, everything else is ditched.
if {Record.is E}
andthen {Record.label E} == element
then
ElementRecord = {ParseElement E}
in
%There should be support for this, Dictionary.append or
something.
{Dictionary.put D {Record.label ElementRecord}
ElementRecord|{Dictionary.condGet D
{Record.label ElementRecord} nil
}
}
end
end
{Dictionary.toRecord Label D}
end
fun {Parsealist AttributeList}
{List.toRecord attributes
{List.map AttributeList
fun {$ Attribute}
AString = {ByteString.toString Attribute.value}
in
%Who needs an XSD? :-))))))
if {String.isInt AString}
then
Attribute.name#{String.toInt AString}
else
Attribute.name#{String.toAtom AString}
end
end
}
}
end
P={New Parser initFromFile('noz.xml')}
{P parse}
{Browser.object browse({ParseElement P.root})}
end
-
Please send submissions to users at mozart-oz.org
and administriva mail to users-request at mozart-oz.org.
The Mozart Oz web site is at http://www.mozart-oz.org/.
More information about the mozart-users
mailing list