Summary:
See also: Built-in Classes, XML Utils, SaxDocumentHandler
The XmlWriter class allows you to write XML documents to different types of output, following the SAX standards.
om.XmlWriter
Class Methods | |
Name | Description |
createFileWriter( file STRING ) RETURNING om.SaxDocumentHandler |
Creates a SaxDocumentHandler object writing to a file. |
createPipeWriter( exec STRING ) RETURNING om.SaxDocumentHandler |
Creates a SaxDocumentHandler object writing to a pipe created for a process. |
createSocketWriter( host STRING,
port STRING ) RETURNING om.SaxDocumentHandler |
Creates a SaxDocumentHandler object writing to a socket. |
This class is only used to create a SaxDocumentHandler object, by using one of the class methods described in the above table.
First, define a variable of type om.SaxDocumentHander and create the object with one of the "create" methods, according to the output destination:
To handle element attributes, define a variable of type om.SaxAttributes.
Create the SaxAttributes object with SaxAttributes.create(). See the class definition for more details about attributes definition.
Use the method startDocument() to start writing to the output. From this point, the order of method calls defines the structure of the XML document.
To write an element, fill the SaxAttributes object with attributes. Then, initiate the element output with the method startElement(name,attset), where name is the tag name of the element and attset is the SaxAttributes object defining element attributes. After this call, you can write text nodes with the characters()method. Finish element output with a call to the endElement() method. Repeat these steps as many times as you have elements to write.
Instead of using the startElement() method, you can generate
processing instruction elements with
processingInstruction(name,attset),
where name is the name of the application.
The resulting XML output is in the form:
<?name attribute="value1" ... ?>
Finally, you must finish the document output with a endDocument() call.
The following code writes an HTML page to a file using the XmlWriter class:
01
MAIN02
DEFINE w om.SaxDocumentHandler03
DEFINE a,n om.SaxAttributes04
05
LET w = om.XmlWriter.createFileWriter("sample.html")06
LET a = om.SaxAttributes.create()07
LET n = om.SaxAttributes.create()08
09
CALL n.clear()10
11
CALL w.startDocument()12
13
CALL w.startElement("HTML",n)14
15
CALL w.startElement("HEAD",n)16
17
CALL w.startElement("TITLE",n)18
CALL w.characters("HTML page generated with XmlWriter")19
CALL w.endElement("TITLE")20
21
CALL a.clear()22
CALL a.addAttribute("type","text/css")23
CALL w.startElement("STYLE",a)24
CALL w.characters("\nBODY { background-color:#c0c0c0; }\n")25
CALL w.endElement("STYLE")26
27
CALL w.endElement("HEAD")28
29
CALL w.startElement("BODY",n)30
31
CALL addHLine(w)32
CALL addTitle(w,"What is XML?",1,"55ff55")33
CALL addParagraph(w,"XML = eXtensible Markup Language ...")34
35
CALL addHLine(w)36
CALL addTitle(w,"What is SAX?",1,"55ff55")37
CALL addParagraph(w,"SAX = Simple Api for XML ...")38
39
CALL w.endElement("BODY")40
41
CALL w.endElement("HTML")42
43
CALL w.endDocument()44
45
END MAIN46
47
FUNCTION addHLine(w)48
DEFINE w om.SaxDocumentHandler49
DEFINE a om.SaxAttributes50
LET a = om.SaxAttributes.create()51
CALL a.clear()52
CALL a.addAttribute("width","100%")53
CALL w.startElement("HR",a)54
CALL w.endElement("HR")55
END FUNCTION56
57
FUNCTION addTitle(w,t,x,c)58
DEFINE w om.SaxDocumentHandler59
DEFINE t VARCHAR(100)60
DEFINE x INTEGER61
DEFINE c VARCHAR(20)62
DEFINE a om.SaxAttributes63
DEFINE n varchar(10)64
LET a = om.SaxAttributes.create()65
LET n = "h" || x66
CALL a.clear()67
CALL w.startElement(n,a)68
IF c IS NOT NULL THEN69
CALL a.addAttribute("color",c)70
END IF71
CALL w.startElement("FONT",a)72
CALL w.characters(t)73
CALL w.endElement("FONT")74
CALL w.endElement(n)75
END FUNCTION76
77
FUNCTION addParagraph(w,t)78
DEFINE w om.SaxDocumentHandler79
DEFINE t VARCHAR(2000)80
DEFINE a om.SaxAttributes81
LET a = om.SaxAttributes.create()82
CALL a.clear()83
CALL w.startElement("P",a)84
CALL w.characters(t)85
CALL w.endElement("P")86
END FUNCTION