XML/DOM serialization: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(10 intermediate revisions by 3 users not shown)
Line 12:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program createXml64.s */
Line 115:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
 
=={{header|ABAP}}==
<syntaxhighlight lang="abap">
<lang ABAP>
DATA: xml_string TYPE string.
 
Line 138:
 
cl_demo_output=>display_text( xml_string ).
</syntaxhighlight>
</lang>
 
Output:
Line 151:
Uses [http://libre.adacore.com/libre/tools/xmlada/ XML/Ada] from AdaCore.
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO.Text_Streams;
with DOM.Core.Documents;
with DOM.Core.Nodes;
Line 174:
N => My_Document,
Pretty_Print => True);
end Serialization;</langsyntaxhighlight>
 
It can be shortened a lot by adding "use" clauses and writing the creation functions into the declaration part.
Line 186:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program createXml.s */
Line 362:
bx lr @ return
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">version = "1.0"
xmlheader := "<?xml version=" . version . "?>" . "<" . root . ">"
 
Line 400:
}
Return xml .= "</" . root%root0% . ">"
}</langsyntaxhighlight>
 
=={{header|Bracmat}}==
To produce the XML including all indentations, we can do this:
<langsyntaxhighlight lang="bracmat">( ("?"."xml version=\"1.0\" ")
\n
(root.,"\n " (element.,"
Line 411:
: ?xml
& out$(toML$!xml)
);</langsyntaxhighlight>
If we do not care about indentation:
<langsyntaxhighlight lang="bracmat">( ("?"."xml version=\"1.0\"") (root.,(element.,"Some text here"))
: ?xml
& out$(toML$!xml)
);</langsyntaxhighlight>
 
=={{header|C}}==
==={{libheader|LibXML}}===
<langsyntaxhighlight lang="c">#include <libxml/parser.h>
#include <libxml/tree.h>
Line 437:
xmlFreeDoc(doc);
xmlCleanupParser();
}</langsyntaxhighlight>
 
==={{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}}==
Serialization using the built-in System.Xml.Serialization library of .Net.
<langsyntaxhighlight lang="csharp">using System.Xml;
using System.Xml.Serialization;
[XmlRoot("root")]
Line 456 ⟶ 541:
}
//Output: <?xml version="1.0" encoding="utf-8"?><root><element>Some text here</element></root>
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{libheader|LibXML}}
<langsyntaxhighlight lang="cpp">
#include <cassert>
#include <cstdlib>
Line 622 ⟶ 707:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Caché ObjectScript}}==
Line 648 ⟶ 733:
{{libheader|clojure.data.xml}}
 
<langsyntaxhighlight lang="clojure">
(require '[clojure.data.xml :as xml])
 
Line 656 ⟶ 741:
(java.io.FileOutputStream. "/tmp/output.xml") "UTF-8")]
(xml/emit xml-example out-file))
</syntaxhighlight>
</lang>
 
{{out}}
Line 667 ⟶ 752:
Assuming that matching the whitespace in the problem description isn't necessary and that character encodings are permitted (see the [[Talk:DOM XML Serialization|talk page]]):
 
<langsyntaxhighlight lang="lisp">(let* ((doc (dom:create-document 'rune-dom:implementation nil nil nil))
(root (dom:create-element doc "root"))
(element (dom:create-element doc "element"))
Line 674 ⟶ 759:
(dom:append-child root element)
(dom:append-child doc root)
(dom:map-document (cxml:make-rod-sink) doc))</langsyntaxhighlight>
 
gives the following output:
Line 683 ⟶ 768:
Because <code>dom:append-child</code> returns the appended child, the same output can be produced while binding fewer variables:
 
<langsyntaxhighlight lang="lisp">(let ((doc (dom:create-document 'rune-dom:implementation nil nil nil)))
(dom:append-child
(dom:append-child
Line 689 ⟶ 774:
(dom:create-element doc "element"))
(dom:create-text-node doc "Some text here"))
(write-string (dom:map-document (cxml:make-rod-sink) doc)))</langsyntaxhighlight>
 
The prefix notation makes this hard to decipher, however, and so a final version uses an auxiliary <code>append-child*</code>:
 
<langsyntaxhighlight lang="lisp">(defun append-child* (parent &rest children)
(reduce 'dom:append-child children :initial-value parent))
 
Line 701 ⟶ 786:
(dom:create-element doc "element")
(dom:create-text-node doc "Some text here"))
(write-string (dom:map-document (cxml:make-rod-sink) doc)))</langsyntaxhighlight>
 
=={{header|D}}==
{{works with|D|2.011+}}
<langsyntaxhighlight lang="d">module xmltest ;
 
import std.stdio ;
Line 716 ⟶ 801:
writefln(doc) ;
// output: <?xml version="1.0"?><root><element>Some text here</element></root>
}</langsyntaxhighlight>
 
=={{header|E}}==
Line 722 ⟶ 807:
This makes use of XML libraries provided with Java.
 
<langsyntaxhighlight lang="e">def document := <unsafe:javax.xml.parsers.makeDocumentBuilderFactory> \
.newInstance() \
.newDocumentBuilder() \
Line 732 ⟶ 817:
element.appendChild(
document.createTextNode("Some text here"))
println(document.saveXML(root))</langsyntaxhighlight>
 
(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#}}==
<langsyntaxhighlight lang="fsharp">open System.Xml
 
[<EntryPoint>]
Line 753 ⟶ 872:
xw.Formatting <- Formatting.Indented
xd.WriteContentTo(xw)
0</langsyntaxhighlight>
Output
<pre><?xml version="1.0"?>
Line 761 ⟶ 880:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: xml.syntax xml.writer ;
 
<XML <root><element>Some text here</element></root> XML>
pprint-xml</langsyntaxhighlight>
{{out}}
<pre>
Line 776 ⟶ 895:
 
Factor's XML syntax isn't just for parsing static XML. Since it supports interpolation (denoted by <code><-></code>) and plays nicely with sequences, it is easy to build up arbitrary trees:
<langsyntaxhighlight lang="factor">USING: sequences xml.syntax xml.writer ;
 
3 [XML <element>Some text here</element> XML] <repetition>
[XML <element2><element3>1.0</element3></element2> XML] append
<XML <root><-></root> XML>
pprint-xml</langsyntaxhighlight>
{{out}}
<pre style="height:60ex">
Line 805 ⟶ 924:
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
using xml
Line 823 ⟶ 942:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 835 ⟶ 954:
=={{header|Forth}}==
{{libheader|Forth Foundation Library}}
<langsyntaxhighlight lang="forth">include ffl/dom.fs
 
\ Create a dom variable 'doc' in the dictionary
Line 863 ⟶ 982:
doc dom-write-string [IF]
type cr
[THEN]</langsyntaxhighlight>
 
=={{header|Go}}==
Line 869 ⟶ 988:
A very small, subset of the W3C DOM Core in Go. The work builds off the existing XML parser. A fork of the godom project (http://code.google.com/p/godom/), which appears to be unsupported at the moment.
 
<langsyntaxhighlight lang="go">package main
 
import (
Line 889 ⟶ 1,008:
}
fmt.Println(string(d.ToXml()))
}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="xml"><root>
<element>
Some text here
</element>
</root></langsyntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">import groovy.xml.MarkupBuilder
def writer = new StringWriter() << '<?xml version="1.0" ?>\n'
def xml = new MarkupBuilder(writer)
Line 904 ⟶ 1,023:
element('Some text here' )
}
println writer</langsyntaxhighlight>
 
=={{header|Haskell}}==
Line 910 ⟶ 1,029:
Using the XML.Light module from [http://hackage.haskell.org/package/xml-1.3.4 HackageDB]
 
<langsyntaxhighlight Haskelllang="haskell">import Data.List
import Text.XML.Light
 
Line 923 ⟶ 1,042:
Nothing
]
Nothing</langsyntaxhighlight>
Output:
<pre>*Main> mapM_ (putStrLn.ppContent) $ parseXML (xmlDOM " Some text ")
Line 934 ⟶ 1,053:
There are probably better ways of doing this, but, here is a simple serializer for a rudimentary concept of a dom:
 
<langsyntaxhighlight lang="j">serialize=: ('<?xml version="1.0" ?>',LF),;@serialize1&''
serialize1=:4 :0
if.L.x do.
Line 944 ⟶ 1,063:
<y,x,LF
end.
)</langsyntaxhighlight>
 
And here is a document object that matches this task:
 
<langsyntaxhighlight lang="j">obj=: 'root';<'element';'some text here'</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight lang="xml"> serialize obj
<?xml version="1.0" ?>
<root>
Line 958 ⟶ 1,077:
some text here
</element>
</root></langsyntaxhighlight>
 
=={{header|Java}}==
[[Java|Java's]] XML DOM tools don't really allow total control of the output format "out of the box" but the following program generates XML that is equivalent to the required output.
<langsyntaxhighlight lang="java">
import java.io.StringWriter;
 
Line 1,068 ⟶ 1,187:
}
}
</syntaxhighlight>
</lang>
 
'''Output:'''
 
<langsyntaxhighlight lang="xml"><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<element>Some text here</element>
</root></langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,081 ⟶ 1,200:
DOM
 
<langsyntaxhighlight lang="javascript">var doc = document.implementation.createDocument( null, 'root', null );
var root = doc.documentElement;
var element = doc.createElement( 'element' );
root.appendChild( element );
element.appendChild( document.createTextNode('Some text here') );
var xmlString = new XMLSerializer().serializeToString( doc );</langsyntaxhighlight>
 
E4X
 
<langsyntaxhighlight lang="javascript">var xml = <root>
<element>Some text here</element>
</root>;
var xmlString = xml.toXMLString();</langsyntaxhighlight>
 
E4X — with processing instruction
 
<langsyntaxhighlight lang="javascript">XML.ignoreProcessingInstructions = false;
var xml = <?xml version="1.0"?>
<root>
<element>Some text here</element>
</root>;
var xmlString = xml.toXMLString();</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using LightXML
 
# Modified from the documentation for LightXML.jl. The underlying library requires an encoding string be printed.
Line 1,123 ⟶ 1,242:
println(xdoc)
 
</langsyntaxhighlight>{{output}}<pre>
<?xml version="1.0" encoding="utf-8"?>
<root>
Line 1,136 ⟶ 1,255:
 
So I've decided to leave it as it is.
<langsyntaxhighlight lang="scala">// version 1.1.3
 
import javax.xml.parsers.DocumentBuilderFactory
Line 1,166 ⟶ 1,285:
}
println(sw)
}</langsyntaxhighlight>
 
{{out}}
Line 1,177 ⟶ 1,296:
 
=={{header|Lasso}}==
<langsyntaxhighlight lang="lasso">
content_type( 'text/xml' );// set MIME type if serving
 
xml( '<?xml version="1.0" ?><root><element>Some text here</element></root>' );
 
</syntaxhighlight>
</lang>
 
=={{header|Lingo}}==
Code based on Flash Xtra (comes with Director, i.e. "built-in") that allows to use AS2/AS3 classes in Lingo (in this case "XML" is an AS2 class).
<langsyntaxhighlight lang="lingo">-- create an XML document
doc = newObject("XML", "<?xml version='1.0' ?>")
 
Line 1,199 ⟶ 1,318:
 
put doc.toString()
-- "<?xml version='1.0' ?><root><element>Some text here</element></root>"</langsyntaxhighlight>
 
=={{header|Lua}}==
Using the widely available 'LuaXML' module
<langsyntaxhighlight Lualang="lua">require("LuaXML")
local dom = xml.new("root")
local element = xml.new("element")
table.insert(element, "Some text here")
dom:append(element)
dom:save("dom.xml")</langsyntaxhighlight>
Resulting contents of dom.xml:
<pre><?xml version="1.0"?>
Line 1,218 ⟶ 1,337:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">DOM = XMLObject["Document"][{XMLObject["Declaration"]["Version" -> "1.0","Encoding" -> "utf-8"]},
XMLElement["root", {}, {XMLElement["element", {}, {"Some text here"}]}], {}];
ExportString[DOM, "XML", "AttributeQuoting" -> "\""]</langsyntaxhighlight>
{{out}}
<pre><?xml version="1.0" encoding="utf-8"?>
Line 1,228 ⟶ 1,347:
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">docNode = com.mathworks.xml.XMLUtils.createDocument('root');
docRootNode = docNode.getDocumentElement;
thisElement = docNode.createElement('element');
thisElement.appendChild(docNode.createTextNode('Some text here'));
docRootNode.appendChild(thisElement);
</syntaxhighlight>
</lang>
Output:
<pre> xmlwrite(docNode)
Line 1,247 ⟶ 1,366:
=={{header|NetRexx}}==
{{trans|Java}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 1,315 ⟶ 1,434:
 
return
</syntaxhighlight>
</lang>
 
'''Output:'''
 
<langsyntaxhighlight lang="xml"><?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<element>Some text here</element>
</root></langsyntaxhighlight>
 
=={{header|Nim}}==
{{libheader|nim-xmldom}}
Note that the module “xmldom” is no longer part of the standard library as more convenient modules are now available. It can be installed using the package manager <code>nimble</code>.
<langsyntaxhighlight lang="nim">import xmldom
 
var
Line 1,339 ⟶ 1,458:
firstElement.appendChild textNode
 
echo document</langsyntaxhighlight>
Output:
<pre><?xml version="1.0" encoding="UTF-8" ?>
Line 1,349 ⟶ 1,468:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">use XML;
 
bundle Default {
Line 1,361 ⟶ 1,480:
}
}
}</langsyntaxhighlight>
 
=={{header|OpenEdge/Progress}}==
The following example uses the X-DOCUMENT, for faster processing SAX can be used.
<langsyntaxhighlight lang="progress">
DEFINE VARIABLE hxdoc AS HANDLE NO-UNDO.
DEFINE VARIABLE hxroot AS HANDLE NO-UNDO.
Line 1,389 ⟶ 1,508:
hxdoc:SAVE( 'LONGCHAR', lcc ).
MESSAGE STRING( lcc ) VIEW-AS ALERT-BOX.
</syntaxhighlight>
</lang>
 
=={{header|Oz}}==
With the code from [[XML Creation#Oz]], we can write:
 
<langsyntaxhighlight lang="oz">declare
proc {Main}
DOM = root(element("Some text here"))
Line 1,400 ⟶ 1,519:
{System.showInfo {Serialize DOM}}
end
...</langsyntaxhighlight>
 
Output:
<langsyntaxhighlight lang="xml"><?xml version="1.0" ?>
<root>
<element>Some text here</element>
</root></langsyntaxhighlight>
 
=={{header|Pascal}}==
{{works with|Free_Pascal}} {{libheader|Classes}} {{libheader|XMLWrite}} {{libheader|DOM}}
<langsyntaxhighlight lang="pascal">program CrearXML;
 
{$mode objfpc}{$H+}
Line 1,435 ⟶ 1,554:
NodoHijo.free;
Xdoc.free;
end.</langsyntaxhighlight>
 
Output:<pre>
Line 1,447 ⟶ 1,566:
 
{{libheader|XML::Simple}}
<langsyntaxhighlight lang="perl">use XML::Simple;
print XMLout( { root => { element => "Some text here" } }, NoAttr => 1, RootName => "" );</langsyntaxhighlight>
 
'''Output''':
Line 1,456 ⟶ 1,575:
 
{{libheader|XML::DOM::BagOfTricks}}
<langsyntaxhighlight lang="perl">use XML::DOM::BagOfTricks qw(createDocument createTextElement);
 
my ($doc, $root) = createDocument('root');
Line 1,462 ⟶ 1,581:
createTextElement($doc, 'element', 'Some text here')
);
print $doc->toString;</langsyntaxhighlight>
 
'''Output''':
Line 1,468 ⟶ 1,587:
 
{{libheader|LibXML}}
<langsyntaxhighlight lang="perl">use XML::LibXML;
 
$xml = XML::LibXML::Document->new('1.0');
Line 1,479 ⟶ 1,598:
$node->addChild($node2);
 
print $xml->toString;</langsyntaxhighlight>
 
'''Output''':
Line 1,486 ⟶ 1,605:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">xml</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,493 ⟶ 1,612:
<span style="color: #000000;">doc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xml_new_doc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">`&lt;?xml version="1.0" ?&gt;`</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">xml_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">doc</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,505 ⟶ 1,624:
=={{header|PHP}}==
{{works with|PHP|5}}
<langsyntaxhighlight lang="php"><?php
$dom = new DOMDocument();//the constructor also takes the version and char-encoding as it's two respective parameters
$dom->formatOutput = true;//format the outputted xml
Line 1,513 ⟶ 1,632:
$root->appendChild($element);
$dom->appendChild($root);
$xmlstring = $dom->saveXML();</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/xm.l")
 
(xml? T)
(xml '(root NIL (element NIL "Some text here")))</langsyntaxhighlight>
Output:
<pre><?xml version="1.0" encoding="utf-8"?>
Line 1,528 ⟶ 1,647:
=={{header|Pike}}==
manually, one node at a time:
<langsyntaxhighlight Pikelang="pike">object dom = Parser.XML.Tree.SimpleRootNode();
dom->add_child(Parser.XML.Tree.SimpleNode(Parser.XML.Tree.XML_HEADER, "", ([]), ""));
object node = Parser.XML.Tree.SimpleNode(Parser.XML.Tree.XML_ELEMENT, "root", ([]), "");
Line 1,541 ⟶ 1,660:
 
dom->render_xml();
Result: "<?xml version='1.0' encoding='utf-8'?><root><element>Some text here</element></root>"</langsyntaxhighlight>
 
from an array, using a conversion function:
<langsyntaxhighlight Pikelang="pike">object make_xml_node(array|string in, void|int level)
{
level++;
Line 1,570 ⟶ 1,689:
array input = ({ "root", ([]), ({ "element", ([]), "Some text here" }) });
make_xml_tree(input)->render_xml();
Result: "<?xml version='1.0' encoding='utf-8'?><root><element>Some text here</element></root>"</langsyntaxhighlight>
 
to render the output with indenting as specified in the task, this function adds the necessary text nodes:
<langsyntaxhighlight Pikelang="pike">object indent_xml(object parent, void|int indent_text, void|int level)
{
int subnodes = false;
Line 1,604 ⟶ 1,723:
" Some text here\r\n"
" </element>\r\n"
"</root>"</langsyntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|2.5}}
<langsyntaxhighlight lang="python">from xml.dom.minidom import getDOMImplementation
 
dom = getDOMImplementation()
Line 1,619 ⟶ 1,738:
firstElement.appendChild(textNode)
 
xmlString = document.toprettyxml(" " * 4)</langsyntaxhighlight>
 
<langsyntaxhighlight lang="python">from xml.etree import ElementTree as et
 
root = et.Element("root")
et.SubElement(root, "element").text = "Some text here"
xmlString = et.tostring(root)</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang at-exp racket
(require xml)
Line 1,645 ⟶ 1,764:
(write-xml xml)
(newline)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 1,651 ⟶ 1,770:
{{works with|Rakudo|2018.05}}
 
<syntaxhighlight lang="raku" perl6line>use XML;
use XML::Writer;
 
say my $document = XML::Document.new(
XML::Writer.serialize( :root[ :element['Some text here', ], ] )
);</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="xml"><?xml version="1.0"?><root><element>Some text here</element></root></langsyntaxhighlight>
 
=={{header|Rascal}}==
<langsyntaxhighlight lang="rascal">import lang::xml::DOM;
 
public void main(){
x = document(element(none(), "root", [element(none(), "element", [charData("Some text here")])]));
return println(xmlPretty(x));
}</langsyntaxhighlight>
Output example:
<langsyntaxhighlight lang="rascal">rascal>main()
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element>Some text here</element>
</root></langsyntaxhighlight>
 
=={{header|Ruby}}==
{{libheader|REXML}}
<langsyntaxhighlight lang="ruby">require("rexml/document")
include REXML
(doc = Document.new) << XMLDecl.new
Line 1,687 ⟶ 1,806:
serialized = String.new
doc.write(serialized, 4)
puts serialized</langsyntaxhighlight>
 
produces
Line 1,698 ⟶ 1,817:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">val xml = <root><element>Some text here</element></root>
scala.xml.XML.save(filename="output.xml", node=xml, enc="UTF-8", xmlDecl=true, doctype=null)</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">require('XML::Simple');
print %S'XML::Simple'.XMLout(
:(root => :( element => 'Some text here' )),
NoAttr => 1, RootName => '',
);</langsyntaxhighlight>
{{out}}
<pre>
Line 1,717 ⟶ 1,836:
=={{header|Tcl}}==
{{libheader|tDOM}}
<langsyntaxhighlight lang="tcl">package require tdom
set d [dom createDocument root]
set root [$d documentElement]
$root appendChild [$d createElement element]
[$root firstChild] appendChild [$d createTextNode "Some text here"]
$d asXML</langsyntaxhighlight>
<pre><root>
<element>Some text here</element>
</root></pre>
Using [http://tclxml.sf.net TclDOM]
<langsyntaxhighlight lang="tcl">package require dom
set doc [dom::DOMImplementation create]
set root [dom::document createElement $doc root]
set elem [dom::document createElement $root element]
set text [dom::document createTextNode $elem "Some text here"]
dom::DOMImplementation serialize $doc -newline {element}</langsyntaxhighlight>
<pre><?xml version='1.0'?>
<!DOCTYPE root>
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.
<langsyntaxhighlight ecmascriptlang="wren">class XmlDocument {
construct new(root) {
_root = root
Line 1,779 ⟶ 1,898:
root.addChild(child)
var doc = XmlDocument.new(root)
System.print(doc)</langsyntaxhighlight>
 
{{out}}
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.
<langsyntaxhighlight ecmascriptlang="wren">import "./xsequence" for XDocument, XElement
 
var doc = XDocument.new(
Line 1,800 ⟶ 1,919:
)
)
System.print(doc)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,810 ⟶ 1,929:
 
=={{header|XProc}}==
<langsyntaxhighlight lang="xml"><p:pipeline xmlns:p="http://www.w3.org/ns/xproc" version="1.0">
<p:identity>
<p:input port="source">
Line 1,822 ⟶ 1,941:
</p:input>
</p:identity>
</p:pipeline></langsyntaxhighlight>
 
=={{header|XQuery}}==
In XQuery static element construction is like normal XML:
<langsyntaxhighlight lang="xquery"><root>
<element>
Some text here
</element>
</root></langsyntaxhighlight>
 
Dynamic element construction looks quite different:
<langsyntaxhighlight lang="xquery">let $rootTagname := 'root'
let $elementTagname := 'element'
let $elementContent := 'Some text here'
Line 1,842 ⟶ 1,961:
element{$elementTagname}
{$elementContent}
}</langsyntaxhighlight>
 
=={{header|XSLT}}==
<langsyntaxhighlight lang="xml"><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/"> <!-- replace the root of the incoming document with our own model -->
Line 1,854 ⟶ 1,973:
</xsl:element>
</xsl:template>
</xsl:stylesheet></langsyntaxhighlight>
 
=={{header|Z80 Assembly}}==
Assembled and run using WinAPE's built-in assembler. Tags have to be manually opened and closed, but it works!
<langsyntaxhighlight lang="z80">org &1000
main:
ld hl,XML_Header
Line 1,953 ⟶ 2,072:
 
org &1300
XMLRam: ;this is where the output is stored.</langsyntaxhighlight>
 
{{out}}
2,130

edits