XML/DOM serialization: Difference between revisions

Added FreeBASIC
m (syntax highlighting fixup automation)
(Added FreeBASIC)
 
(9 intermediate revisions by 2 users not shown)
Line 419:
 
=={{header|C}}==
==={{libheader|LibXML}}===
<syntaxhighlight lang="c">#include <libxml/parser.h>
#include <libxml/tree.h>
Line 438:
xmlCleanupParser();
}</syntaxhighlight>
 
==={{libheader|Gadget}}===
<p>Version 1 (48 allocs):</p>
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
 
LIB_GADGET_START
 
Main
String XML, body;
Stack{
Store ( XML, Parser( "element", "","Some text here", NORMAL_TAG) );
Store ( XML, Parser( "root", "",XML, NORMAL_TAG) );
} Stack_off;
body = Multi_copy( body,"<?xml version=\"1.0\" ?>", XML, NULL);
Print "%s\n", body;
Free secure XML, body;
End
</syntaxhighlight>
<p>Version 2 (46 allocs):</p>
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
 
LIB_GADGET_START
 
Main
String XML, body;
Get_fn_let( XML, Parser( "element", "","Some text here", NORMAL_TAG) );
Get_fn_let( XML, Parser( "root", "",XML, NORMAL_TAG) );
body = Multi_copy( body,"<?xml version=\"1.0\" ?>", XML, NULL);
Print "%s\n", body;
Free secure XML, body;
End
</syntaxhighlight>
<p>Version 3 (47 allocs):</p>
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
 
LIB_GADGET_START
 
Main
String XML, body;
Stack{
Store ( XML, Parser( "root", "",
Parser( "element", "","Some text here", NORMAL_TAG),
NORMAL_TAG) );
} Stack_off;
body = Multi_copy( body,"<?xml version=\"1.0\" ?>", XML, NULL);
Print "%s\n", body;
Free secure XML, body;
End
</syntaxhighlight>
 
<p>Version 4 (50 allocs):</p>
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
 
LIB_GADGET_START
 
Main
String body;
Stack{
Store ( body, Multi_copy( body,"<?xml version=\"1.0\" ?>",
Parser( "root", "", Parser( "element", "","Some text here",
NORMAL_TAG), NORMAL_TAG),
NULL) );
}
 
Print "%s\n", body;
Free secure body;
End
</syntaxhighlight>
{{out}}
<pre>
$ ./tests/RC_dom
<?xml version="1.0" ?><root><element>Some text here</element></root>
</pre>
 
=={{header|C sharp}}==
Line 735 ⟶ 820:
 
(On the use of <tt>&lt;unsafe></tt>: The class has not yet been reviewed for E safety, so <tt>&lt;import:...makeDocumentBuilderFactory></tt> is not yet allowed. The review would probably be straightforward.)
 
=={{header|FreeBASIC}}==
{{libheader|libxml2}}
<syntaxhighlight lang="vbnet">#include once "crt/stdio.bi"
#include once "crt/stdlib.bi"
#include once "libxml/tree.bi"
#include once "libxml/parser.bi"
 
'' Create a new document
Dim As xmlDocPtr doc = xmlNewDoc(Strptr("1.0"))
 
'' Create a root node
Dim As xmlNodePtr root_node = xmlNewNode(NULL, Strptr("root"))
 
'' Set the root node for the document
xmlDocSetRootElement(doc, root_node)
 
'' Create a new node
Dim As xmlNodePtr node = xmlNewNode(NULL, Strptr("element"))
 
'' Add some text to the node
xmlNodeSetContent(node, Strptr("Some text here"))
 
'' Add the new node to the root node
xmlAddChild(root_node, node)
 
'' Dump the document to stdout, it will be well formatted
xmlDocDump(stdout, doc)
 
'' Free the document
xmlFreeDoc(doc)
 
'' Free the global variables that may have been allocated by the parser
xmlCleanupParser()</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 1,743 ⟶ 1,862:
=={{header|Wren}}==
Wren has no built-in or (AFAIK) third party support for XML so we code a minimal DOM sufficient for completing this task.
<syntaxhighlight lang="ecmascriptwren">class XmlDocument {
construct new(root) {
_root = root
Line 1,793 ⟶ 1,912:
{{libheader|Wren-xsequence}}
Since the first version was written, the above XML parser has appeared and, consequently, the solution can now be rewritten as follows.
<syntaxhighlight lang="ecmascriptwren">import "./xsequence" for XDocument, XElement
 
var doc = XDocument.new(
2,130

edits