XML/DOM serialization: Difference between revisions
Content deleted Content added
→{{header|Go}}: Bitbucket.org/rj/xmldom-go was deleted but found alternative version of it on https://gitlab.com/stone.code/xmldom-go.git. I tested the code runs fine with updated lib link. |
PascalABC.NET |
||
(18 intermediate revisions by 9 users not shown) | |||
Line 12:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight 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>
=={{header|ABAP}}==
<syntaxhighlight lang="abap">
DATA: xml_string TYPE string.
Line 138:
cl_demo_output=>display_text( xml_string ).
</syntaxhighlight>
Output:
Line 151:
Uses [http://libre.adacore.com/libre/tools/xmlada/ XML/Ada] from AdaCore.
<
with DOM.Core.Documents;
with DOM.Core.Nodes;
Line 174:
N => My_Document,
Pretty_Print => True);
end Serialization;</
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">
/* ARM assembly Raspberry PI */
/* program createXml.s */
Line 362:
bx lr @ return
</syntaxhighlight>
=={{header|AutoHotkey}}==
<
xmlheader := "<?xml version=" . version . "?>" . "<" . root . ">"
Line 400:
}
Return xml .= "</" . root%root0% . ">"
}</
=={{header|Bracmat}}==
To produce the XML including all indentations, we can do this:
<
\n
(root.,"\n " (element.,"
Line 411:
: ?xml
& out$(toML$!xml)
);</
If we do not care about indentation:
<
: ?xml
& out$(toML$!xml)
);</
=={{header|C}}==
==={{libheader|LibXML}}===
<
#include <libxml/tree.h>
Line 437:
xmlFreeDoc(doc);
xmlCleanupParser();
}</
==={{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.
<
using System.Xml.Serialization;
[XmlRoot("root")]
Line 456 ⟶ 541:
}
//Output: <?xml version="1.0" encoding="utf-8"?><root><element>Some text here</element></root>
}</
=={{header|C++}}==
{{libheader|LibXML}}
<
#include <cassert>
#include <cstdlib>
Line 622 ⟶ 707:
}
}
</syntaxhighlight>
=={{header|Caché ObjectScript}}==
Line 648 ⟶ 733:
{{libheader|clojure.data.xml}}
<
(require '[clojure.data.xml :as xml])
Line 656 ⟶ 741:
(java.io.FileOutputStream. "/tmp/output.xml") "UTF-8")]
(xml/emit xml-example out-file))
</syntaxhighlight>
{{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]]):
<
(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))</
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:
<
(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)))</
The prefix notation makes this hard to decipher, however, and so a final version uses an auxiliary <code>append-child*</code>:
<
(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)))</
=={{header|D}}==
{{works with|D|2.011+}}
<
import std.stdio ;
Line 716 ⟶ 801:
writefln(doc) ;
// output: <?xml version="1.0"?><root><element>Some text here</element></root>
}</
=={{header|E}}==
Line 722 ⟶ 807:
This makes use of XML libraries provided with Java.
<
.newInstance() \
.newDocumentBuilder() \
Line 732 ⟶ 817:
element.appendChild(
document.createTextNode("Some text here"))
println(document.saveXML(root))</
(On the use of <tt><unsafe></tt>: The class has not yet been reviewed for E safety, so <tt><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#}}==
<
[<EntryPoint>]
Line 753 ⟶ 872:
xw.Formatting <- Formatting.Indented
xd.WriteContentTo(xw)
0</
Output
<pre><?xml version="1.0"?>
Line 761 ⟶ 880:
=={{header|Factor}}==
<
<XML <root><element>Some text here</element></root> XML>
pprint-xml</
{{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:
<
3 [XML <element>Some text here</element> XML] <repetition>
[XML <element2><element3>1.0</element3></element2> XML] append
<XML <root><-></root> XML>
pprint-xml</
{{out}}
<pre style="height:60ex">
Line 805 ⟶ 924:
=={{header|Fantom}}==
<
using xml
Line 823 ⟶ 942:
}
}
</syntaxhighlight>
Output:
Line 835 ⟶ 954:
=={{header|Forth}}==
{{libheader|Forth Foundation Library}}
<
\ Create a dom variable 'doc' in the dictionary
Line 863 ⟶ 982:
doc dom-write-string [IF]
type cr
[THEN]</
=={{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.
<
import (
Line 889 ⟶ 1,008:
}
fmt.Println(string(d.ToXml()))
}</
{{out}}
<
<element>
Some text here
</element>
</root></
=={{header|Groovy}}==
<
def writer = new StringWriter() << '<?xml version="1.0" ?>\n'
def xml = new MarkupBuilder(writer)
Line 904 ⟶ 1,023:
element('Some text here' )
}
println writer</
=={{header|Haskell}}==
Line 910 ⟶ 1,029:
Using the XML.Light module from [http://hackage.haskell.org/package/xml-1.3.4 HackageDB]
<
import Text.XML.Light
Line 923 ⟶ 1,042:
Nothing
]
Nothing</
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:
<
serialize1=:4 :0
if.L.x do.
Line 944 ⟶ 1,063:
<y,x,LF
end.
)</
And here is a document object that matches this task:
<
Example use:
<
<?xml version="1.0" ?>
<root>
Line 958 ⟶ 1,077:
some text here
</element>
</root></
=={{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.
<
import java.io.StringWriter;
Line 1,068 ⟶ 1,187:
}
}
</syntaxhighlight>
'''Output:'''
<
<root>
<element>Some text here</element>
</root></
=={{header|JavaScript}}==
Line 1,081 ⟶ 1,200:
DOM
<
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 );</
E4X
<
<element>Some text here</element>
</root>;
var xmlString = xml.toXMLString();</
E4X — with processing instruction
<
var xml = <?xml version="1.0"?>
<root>
<element>Some text here</element>
</root>;
var xmlString = xml.toXMLString();</
=={{header|Julia}}==
<
# Modified from the documentation for LightXML.jl. The underlying library requires an encoding string be printed.
Line 1,123 ⟶ 1,242:
println(xdoc)
</
<?xml version="1.0" encoding="utf-8"?>
<root>
Line 1,136 ⟶ 1,255:
So I've decided to leave it as it is.
<
import javax.xml.parsers.DocumentBuilderFactory
Line 1,166 ⟶ 1,285:
}
println(sw)
}</
{{out}}
Line 1,177 ⟶ 1,296:
=={{header|Lasso}}==
<
content_type( 'text/xml' );// set MIME type if serving
xml( '<?xml version="1.0" ?><root><element>Some text here</element></root>' );
</syntaxhighlight>
=={{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).
<
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>"</
=={{header|Lua}}==
Using the widely available 'LuaXML' module
<
local dom = xml.new("root")
local element = xml.new("element")
table.insert(element, "Some text here")
dom:append(element)
dom:save("dom.xml")</
Resulting contents of dom.xml:
<pre><?xml version="1.0"?>
Line 1,217 ⟶ 1,336:
</root></pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
XMLElement["root", {}, {XMLElement["element", {}, {"Some text here"}]}], {}];
ExportString[DOM, "XML", "AttributeQuoting" -> "\""]</syntaxhighlight>
{{out}}
<pre><?xml version="1.0" encoding="utf-8"?>
<root>
Line 1,229 ⟶ 1,347:
=={{header|MATLAB}}==
<
docRootNode = docNode.getDocumentElement;
thisElement = docNode.createElement('element');
thisElement.appendChild(docNode.createTextNode('Some text here'));
docRootNode.appendChild(thisElement);
</syntaxhighlight>
Output:
<pre> xmlwrite(docNode)
Line 1,248 ⟶ 1,366:
=={{header|NetRexx}}==
{{trans|Java}}
<
options replace format comments java crossref symbols nobinary
Line 1,316 ⟶ 1,434:
return
</syntaxhighlight>
'''Output:'''
<
<root>
<element>Some text here</element>
</root></
=={{header|Nim}}==
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>.
<syntaxhighlight lang="nim">import xmldom
var
Line 1,338 ⟶ 1,458:
firstElement.appendChild textNode
echo document</
Output:
<pre><?xml version="1.0" encoding="UTF-8" ?>
Line 1,348 ⟶ 1,468:
=={{header|Objeck}}==
<
bundle Default {
Line 1,360 ⟶ 1,480:
}
}
}</
=={{header|OpenEdge/Progress}}==
The following example uses the X-DOCUMENT, for faster processing SAX can be used.
<
DEFINE VARIABLE hxdoc AS HANDLE NO-UNDO.
DEFINE VARIABLE hxroot AS HANDLE NO-UNDO.
Line 1,388 ⟶ 1,508:
hxdoc:SAVE( 'LONGCHAR', lcc ).
MESSAGE STRING( lcc ) VIEW-AS ALERT-BOX.
</syntaxhighlight>
=={{header|Oz}}==
With the code from [[XML Creation#Oz]], we can write:
<
proc {Main}
DOM = root(element("Some text here"))
Line 1,399 ⟶ 1,519:
{System.showInfo {Serialize DOM}}
end
...</
Output:
<
<root>
<element>Some text here</element>
</root></
=={{header|Pascal}}==
{{works with|Free_Pascal}} {{libheader|Classes}} {{libheader|XMLWrite}} {{libheader|DOM}}
<
{$mode objfpc}{$H+}
Line 1,430 ⟶ 1,550:
NodoRaiz.AppendChild(NodoPadre); // insertar el nodo hijo en el correspondiente nodo padre
writeXMLFile(xDoc,'prueba.xml'); // escribir el XML
NodoRaiz.free;
NodoPadre.free;
NodoHijo.free;
Xdoc.free;
end.</
Output:<pre>
Line 1,439 ⟶ 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}}==
{{libheader|XML::Simple}}
<
print XMLout( { root => { element => "Some text here" } }, NoAttr => 1, RootName => "" );</
'''Output''':
Line 1,452 ⟶ 1,603:
{{libheader|XML::DOM::BagOfTricks}}
<
my ($doc, $root) = createDocument('root');
Line 1,458 ⟶ 1,609:
createTextElement($doc, 'element', 'Some text here')
);
print $doc->toString;</
'''Output''':
Line 1,464 ⟶ 1,615:
{{libheader|LibXML}}
<
$xml = XML::LibXML::Document->new('1.0');
Line 1,475 ⟶ 1,626:
$node->addChild($node2);
print $xml->toString;</
'''Output''':
Line 1,482 ⟶ 1,633:
=={{header|Phix}}==
<!--<syntaxhighlight lang="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>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">elem</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xml_new_element</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"element"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Some text here"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">root</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xml_new_element</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"root"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">elem</span><span style="color: #0000FF;">}),</span>
<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;">`<?xml version="1.0" ?>`</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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,498 ⟶ 1,652:
=={{header|PHP}}==
{{works with|PHP|5}}
<
$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,506 ⟶ 1,660:
$root->appendChild($element);
$dom->appendChild($root);
$xmlstring = $dom->saveXML();</
=={{header|PicoLisp}}==
<
(xml? T)
(xml '(root NIL (element NIL "Some text here")))</
Output:
<pre><?xml version="1.0" encoding="utf-8"?>
Line 1,521 ⟶ 1,675:
=={{header|Pike}}==
manually, one node at a time:
<
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,534 ⟶ 1,688:
dom->render_xml();
Result: "<?xml version='1.0' encoding='utf-8'?><root><element>Some text here</element></root>"</
from an array, using a conversion function:
<
{
level++;
Line 1,563 ⟶ 1,717:
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>"</
to render the output with indenting as specified in the task, this function adds the necessary text nodes:
<
{
int subnodes = false;
Line 1,597 ⟶ 1,751:
" Some text here\r\n"
" </element>\r\n"
"</root>"</
=={{header|Python}}==
{{works with|Python|2.5}}
<
dom = getDOMImplementation()
Line 1,612 ⟶ 1,766:
firstElement.appendChild(textNode)
xmlString = document.toprettyxml(" " * 4)</
<
root = et.Element("root")
et.SubElement(root, "element").text = "Some text here"
xmlString = et.tostring(root)</
=={{header|Racket}}==
<
#lang at-exp racket
(require xml)
Line 1,638 ⟶ 1,792:
(write-xml xml)
(newline)
</syntaxhighlight>
=={{header|Raku}}==
Line 1,644 ⟶ 1,798:
{{works with|Rakudo|2018.05}}
<syntaxhighlight lang="raku"
use XML::Writer;
say my $document = XML::Document.new(
XML::Writer.serialize( :root[ :element['Some text here', ], ] )
);</
{{out}}
<
=={{header|Rascal}}==
<
public void main(){
x = document(element(none(), "root", [element(none(), "element", [charData("Some text here")])]));
return println(xmlPretty(x));
}</
Output example:
<
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element>Some text here</element>
</root></
=={{header|Ruby}}==
{{libheader|REXML}}
<
include REXML
(doc = Document.new) << XMLDecl.new
Line 1,680 ⟶ 1,834:
serialized = String.new
doc.write(serialized, 4)
puts serialized</
produces
Line 1,691 ⟶ 1,845:
=={{header|Scala}}==
<
scala.xml.XML.save(filename="output.xml", node=xml, enc="UTF-8", xmlDecl=true, doctype=null)</
=={{header|Sidef}}==
{{trans|Perl}}
<
print %S'XML::Simple'.XMLout(
:(root => :( element => 'Some text here' )),
NoAttr => 1, RootName => '',
);</
{{out}}
<pre>
Line 1,710 ⟶ 1,864:
=={{header|Tcl}}==
{{libheader|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</
<pre><root>
<element>Some text here</element>
</root></pre>
Using [http://tclxml.sf.net TclDOM]
<
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}</
<pre><?xml version='1.0'?>
<!DOCTYPE root>
Line 1,733 ⟶ 1,887:
</element>
</root></pre>
=={{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="wren">class XmlDocument {
construct new(root) {
_root = root
}
toString { "<?xml version=\"1.0\" ?>\n%(_root.toString(0))" }
}
class XmlElement {
construct new(name, text) {
_name = name
_text = text
_children = []
}
name { _name }
text { _text }
children { _children }
addChild(child) { _children.add(child) }
toString(level) {
var indent = " "
var s = indent * level + "<%(name)>\n"
if (_text != "") s = s + indent * (level + 1) + _text + "\n"
for (c in _children) {
s = s + c.toString(level+1) + "\n"
}
return s + indent * level + "</%(name)>"
}
}
var root = XmlElement.new("root", "")
var child = XmlElement.new("element", "Some text here")
root.addChild(child)
var doc = XmlDocument.new(root)
System.print(doc)</syntaxhighlight>
{{out}}
<pre>
<?xml version="1.0" ?>
<root>
<element>
Some text here
</element>
</root>
</pre>
<br>
{{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="wren">import "./xsequence" for XDocument, XElement
var doc = XDocument.new(
XElement.new("root",
XElement.new("element", "Some text here")
)
)
System.print(doc)</syntaxhighlight>
{{out}}
<pre>
<?xml version="1.0" encoding="utf-8"?>
<root>
<element>Some text here</element>
</root>
</pre>
=={{header|XProc}}==
<
<p:identity>
<p:input port="source">
Line 1,747 ⟶ 1,969:
</p:input>
</p:identity>
</p:pipeline></
=={{header|XQuery}}==
In XQuery static element construction is like normal XML:
<
<element>
Some text here
</element>
</root></
Dynamic element construction looks quite different:
<
let $elementTagname := 'element'
let $elementContent := 'Some text here'
Line 1,767 ⟶ 1,989:
element{$elementTagname}
{$elementContent}
}</
=={{header|XSLT}}==
<
<xsl:output method="xml" indent="yes" />
<xsl:template match="/"> <!-- replace the root of the incoming document with our own model -->
Line 1,779 ⟶ 2,001:
</xsl:element>
</xsl:template>
</xsl:stylesheet></
=={{header|Z80 Assembly}}==
Assembled and run using WinAPE's built-in assembler. Tags have to be manually opened and closed, but it works!
<syntaxhighlight lang="z80">org &1000
main:
ld hl,XML_Header
ld de,XMLRam
call strcpy
ld hl,XML_Root
push hl
call AddXMLNode
ld hl,XML_Element
push hl
call AddXMLNode
ld hl,XML_Entry
call strcpy
pop hl ;ld hl,XML_Element
call CloseXMLNode
pop hl ;ld hl,XML_Root
call CloseXMLNode
ld hl,XMLRam
jp PrintString ;and then return to basic.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
AddXMLNode:
;hl = pointer to desired node name
;de = destination ram
;carry flag; set = <foo/>, clear = <foo></foo>
push af
ld a,'<'
ld (de),a
inc de
call strcpy
pop af
jr nc,skip_AddXMLNode
ld a,'/'
ld (de),a
inc de
skip_AddXMLNode:
ld a,'>'
ld (de),a
inc de
xor a
ld (de),a
;don't inc de afterwards, since we may want to add more
ret
CloseXMLNode:
ld a,'<'
ld (de),a
inc de
ld a,'/'
ld (de),a
inc de
call strcpy
ld a,'>'
ld (de),a
inc de
xor a
ld (de),a
ret
PrintString:
ld a,(hl)
or a
ret z
call &BB5A
inc hl
jr PrintString
strcpy:
;HL = string source
;DE = destination
;copies 1 byte at a time until a null terminator is copied, then exits.
ld a,(hl)
ld (de),a
or a
ret z
inc hl
inc de
jp strcpy
org &1200
XML_Header:
byte "<?xml version=",&22,"1.0",&22,"?>",0
XML_Root:
byte "root",0
XML_Element:
byte "element",0
XML_Entry:
byte "some text here",0
org &1300
XMLRam: ;this is where the output is stored.</syntaxhighlight>
{{out}}
<pre>call &1000
<?xml version="1.0"?><root><element>some text here</element></root></pre>
{{omit from|Batch File|No way of XML parsing or processing.}}
|