XML/DOM serialization: Difference between revisions

Content deleted Content added
Mr Dalien (talk | contribs)
Miks1965 (talk | contribs)
PascalABC.NET
 
(6 intermediate revisions by 3 users not shown)
Line 477:
End
</syntaxhighlight>
<p>Version 3 (5747 allocs):</p>
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
Line 495:
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>
Line 799 ⟶ 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,507 ⟶ 1,562:
</root>.
</pre>
 
=={{header|PascalABC.NET}}==
See [[C#]]
<syntaxhighlight lang="delphi">
{$reference System.Xml.Serialization.dll}
{$reference System.Xml.dll}
uses System.Xml;
uses System.Xml.Serialization;
 
type
[XmlRoot('root')]
ExampleXML = class
public
[XmlElementAttribute('element')]
element := 'My text';
end;
 
begin
var xmlnamespace := new XmlSerializerNamespaces();
xmlnamespace.Add('', '');
var writer := XmlWriter.Create('output.xml');
(new XmlSerializer(typeof(ExampleXML))).Serialize(writer, new ExampleXML(), xmlnamespace);
end.</syntaxhighlight>
{{out}}
<pre>
<?xml version="1.0" encoding="utf-8"?><root><element>My text</element></root>
</pre>
 
 
=={{header|Perl}}==
Line 1,807 ⟶ 1,890:
=={{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,857 ⟶ 1,940:
{{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(