XML¶
Introduction¶
FWD provides complete support for 4GL eXtensible Markup Language constructs.
DOM - Document Object Model¶
The below 4GL snippet shows some X-DOCUMENT DOM capabilities:
create x-document h.
create x-noderef hr.
create x-noderef h1.
create x-noderef h2.
h:create-node(hr, "lvl1", "element").
h:append-child(hr).
h:create-node(h1, "lvl2", "element").
hr:append-child(h1).
h1:set-attribute("fwd", "true").
h:create-node(h2, "", "cdata-section").
h2:node-value = "This is some text".
h1:append-child(h2).
h:save("file", "a.xml").
And the pertinent converted Java:
XmlFactory.createXDocument(h);
XmlFactory.createXNodeReference(hr);
XmlFactory.createXNodeReference(h1);
XmlFactory.createXNodeReference(h2);
h.unwrapXDocument().createNode(hr, "lvl1", "element");
h.unwrapXEntity().appendChild(hr);
h.unwrapXDocument().createNode(h1, "lvl2", "element");
hr.unwrapXEntity().appendChild(h1);
h1.unwrapXNodeRef().setAttribute("fwd", "true");
h.unwrapXDocument().createNode(h2, "", "cdata-section");
h2.unwrapXNodeRef().setNodeValue(new character("This is some text"));
h1.unwrapXEntity().appendChild(h2);
h.unwrapXDocument().save("file", "a.xml");
SAX¶
A simple SAX-reader sample:
procedure StartElement:
define input parameter namespaceURI as character.
define input parameter localName as character.
define input parameter qname as character.
define input parameter attributes as HANDLE.
def var c as char.
c = attributes:get-value-by-qname("value").
if c <> "testing" then message "Unexpected error 1" c.
c = attributes:get-value-by-qname("S1:parent").
if c <> "http://www.mywebsite.com/a.html" then message "Unexpected error 2" c.
c = attributes:get-value-by-qname("s1:parent").
if c <> ? then message "Unexpected error 3" c.
c = attributes:get-value-by-qname("parent").
if c <> ? then message "Unexpected error 4" c.
c = attributes:get-value-by-qname("xmlns:parent").
if c <> ? then message "Unexpected error 5" c.
c = attributes:get-value-by-qname("xmlns:url").
if c <> ? then message "Unexpected error 6" c.
c = attributes:get-value-by-qname("url").
if c <> ? then message "Unexpected error 7" c.
def var ni as integer.
ni = attributes:num-items.
def var i as int.
def var ln as char.
def var qn as char.
def var type as char.
def var uri as char.
def var val as char.
do i = 1 to ni:
ln = attributes:get-localname-by-index(i).
qn = attributes:get-QNAME-by-index(i).
type = attributes:get-TYPE-by-index(i).
uri = attributes:get-URI-by-index(i).
val = attributes:get-VALUE-by-index(i).
if i = 1 then do:
if ln <> "parent" then message "failed ln" i ln qn type uri val.
if qn <> "S1:parent" then message "failed qn" i ln qn type uri val.
if type <> "CDATA" then message "failed type" i ln qn type uri val.
if uri <> "urn:soap-fault:details" then message "failed uri" i ln qn type uri val.
if val <> "http://www.mywebsite.com/a.html" then message "failed val" i ln qn type uri val.
if attributes:get-index-by-qname(qn) <> i then message "failed g1" i ln qn type uri val.
if attributes:get-value-by-qname(qn) <> val then message "failed g2" i ln qn type uri val.
if attributes:get-type-by-qname(qn) <> type then message "failed g3" i ln qn type uri val.
end.
else if i = 2 then do:
if ln <> "value" then message "failed ln" i ln qn type uri val.
if qn <> "value" then message "failed qn" i ln qn type uri val.
if type <> "CDATA" then message "failed type" i ln qn type uri val.
if uri <> "" then message "failed uri" i ln qn type uri val.
if val <> "testing" then message "failed val" i ln qn type uri val.
if attributes:get-index-by-qname(qn) <> i then message "failed g1" i ln qn type uri val.
if attributes:get-value-by-qname(qn) <> val then message "failed g2" i ln qn type uri val.
if attributes:get-type-by-qname(qn) <> type then message "failed g3" i ln qn type uri val.
end.
else do:
message "unexpected" i ln qn type uri val.
end.
end.
end procedure.
def var saxReader as handle.
def var hHandle as handle.
create sax-reader saxReader.
saxReader:handler = this-procedure.
def var lc1 as longchar.
lc1 = "<fwd xmlns='http://www.mywebsite.com/default' xmlns:S1='urn:soap-fault:details' S1:parent='http://www.mywebsite.com/a.html' value='testing' xmlns:url='http://www.mywebsite.com/b.html'>this is just a test</fwd>".
saxReader:set-input-source("longchar", lc1).
saxReader:suppress-namespace-processing = false.
saxReader:sax-parse().
The converted Java pertinent parts:
/**
* External procedure (converted to Java from the 4GL source code
* in xml/sax/simple_sax_test.p).
*/
@LegacySignature(type = Type.MAIN, name = "xml/sax/simple_sax_test.p")
public void execute()
{
externalProcedure(SimpleSaxTest.this, new Block((Body) () ->
{
XmlFactory.createSaxReader(saxReader);
saxReader.unwrapSaxReader().setHandler(thisProcedure());
lc1.assign(new character("<fwd xmlns='http://www.mywebsite.com/default' xmlns:S1='urn:soap-fault:details' S1:parent='http://www.mywebsite.com/a.html' value='testing' xmlns:url='http://www.mywebsite.com/b.html'>this is just a test</fwd>"));
saxReader.unwrapSaxReader().setInputSource("longchar", lc1);
saxReader.unwrapXmlSchema().setSuppressNamespaceProcessing(new logical(false));
saxReader.unwrapSaxReader().saxParse();
}));
}
.
.
.
public void startElement(final character _namespaceUri, final character _localName, final character _qname, final handle _attributes)
{
character namespaceUri = UndoableFactory.initInput(_namespaceUri);
character localName = UndoableFactory.initInput(_localName);
character qname = UndoableFactory.initInput(_qname);
handle attributes = UndoableFactory.initInput(_attributes);
character c = UndoableFactory.character();
integer ni = UndoableFactory.integer();
integer i = UndoableFactory.integer();
character ln = UndoableFactory.character();
character qn = UndoableFactory.character();
character type = UndoableFactory.character();
character uri = UndoableFactory.character();
character val = UndoableFactory.character();
internalProcedure(new Block((Body) () ->
{
c.assign(attributes.unwrapSaxAttributes().getValueByQname("value"));
if (_isNotEqual(c, "testing"))
{
message(new Object[]
{
"Unexpected error 1",
c
});
}
c.assign(attributes.unwrapSaxAttributes().getValueByQname("S1:parent"));
.
.
.
© 2004-2022 Golden Code Development Corporation. ALL RIGHTS RESERVED.