Generating and reading XML doc
stéphane ducasse
ducasse at iam.unibe.ch
Tue Dec 7 15:00:30 UTC 2004
thanks a lot boris
Thanks thanks ....
I will have a look at that.
Stef
On 7 déc. 04, at 14:53, Boris Gaertner wrote:
> From: "stéphane ducasse" <ducasse at iam.unibe.ch>
>
>> hi all
>>
>> does someone has an example/application from which I could learn how
>> to
>> programmatically
>> save XML code and read them back?
> For the beginning, do it the other way round:
> Read a piece of XML and save it again. That's easier.
>
> The world is full of XML and some xml files are both small and
> understandable. I recommend svg files for a first contact to XML.
> svg (scaleable vector graphics) is a W3C recommendation that
> some of the newer internet browsers support. When you download
> the SVG documentation from http://www.w3.org/TR/SVG/
> you get more than enough small xml files to play with.
>
> With Squeak, try this:
>
> Copy the attached file line01.svg into your working directory
> and evaluate the following expression with inspect:
>
> XMLDOMParser parseDocumentFrom: (FileDirectory default oldFileNamed:
> 'line01.svg')
>
> The inspector shows you an instance of XMLDocument. The
> subelements are in an OrderedCollection that is the value
> of instance variable 'elements'.
>
>
> Next try this:
>
> | elem |
> elem := XMLDOMParser parseDocumentFrom: (FileDirectory default
> oldFileNamed: 'line01.svg').
> elem printOn: (FileDirectory default newFileNamed: 'output.svg')
>
> When you have done this, you have the file output.svg in your working
> directory. Inspect it with with your favourite editor: It is XML!
>
> If you have a good internet browser, you can doubleclick an svg
> file to see its contents. Try both line01.svg and output.svg
> to see that it is the same drawing.
>
> --- This ends your first attempt to use XML ----
>
> For serious work, you have to do with instances of XMLDocument.
> XMLDocument inherits most of its protocol from
> XMLNodeWithElements. It is there that you find the
> methods that you need to create a document.
>
> The next example shows that it is easy to create an
> XMLDocument and to file it out:
>
> | doc elem elem2 elem3 |
>
> doc := XMLDocument new.
> elem := XMLElement
> named: #svg
> attributes:
> (Dictionary new
> at: 'width' put: '14cm';
> at: 'height' put: '8cm';
> at: 'viewbox' put: '0 0 1200 400';
> yourself).
> elem2 := XMLElement
> named: #rect
> attributes:
> (Dictionary new
> at: 'fill' put: 'green';
> at: 'height' put: '200';
> at: 'width' put: '400';
> at: 'x' put: '50';
> at: 'y' put: '50';
> at: 'stroke' put: 'red';
> at: 'stroke-width' put: '6';
> yourself).
> elem3 := XMLElement
> named: #text
> attributes:
> (Dictionary new
> at: 'font-size' put: '80';
> at: 'stroke' put: 'none';
> at: 'fill' put: 'white';
> at: 'x' put: '80';
> at: 'y' put: '150';
> yourself).
> elem3 addElement: (XMLStringNode string: 'Squeak').
>
> elem addElement: elem2;
> addElement: elem3.
> doc addElement: elem.
> doc version: '1.0'.
> doc printOn: (FileDirectory default newFileNamed: 'firstImage.svg')
>
>
>
>
> As this is again a svg drawing (it draws a green rectangle with a red
> border and a text in white), you can try to view it with a suitable
> viewer.
>
> The attributes of the elements #svg, #rect, #text are described
> in the before-mentioned W3C document. For serious work, you will
> allways need a document that describes the properties of the
> elements that you use.
>
> Enjoy,
> Boris
> <line01.svg>
More information about the Squeak-dev
mailing list
|