all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
8
Task/XML-DOM-serialization/0DESCRIPTION
Normal file
8
Task/XML-DOM-serialization/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Create a simple DOM and having it serialize to:
|
||||
|
||||
<?xml version="1.0" ?>
|
||||
<root>
|
||||
<element>
|
||||
Some text here
|
||||
</element>
|
||||
</root>
|
||||
2
Task/XML-DOM-serialization/1META.yaml
Normal file
2
Task/XML-DOM-serialization/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: XML
|
||||
24
Task/XML-DOM-serialization/Ada/xml-dom-serialization.ada
Normal file
24
Task/XML-DOM-serialization/Ada/xml-dom-serialization.ada
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
with Ada.Text_IO.Text_Streams;
|
||||
with DOM.Core.Documents;
|
||||
with DOM.Core.Nodes;
|
||||
|
||||
procedure Serialization is
|
||||
My_Implementation : DOM.Core.DOM_Implementation;
|
||||
My_Document : DOM.Core.Document;
|
||||
My_Root_Node : DOM.Core.Element;
|
||||
My_Element_Node : DOM.Core.Element;
|
||||
My_Text_Node : DOM.Core.Text;
|
||||
begin
|
||||
My_Document := DOM.Core.Create_Document (My_Implementation);
|
||||
My_Root_Node := DOM.Core.Documents.Create_Element (My_Document, "root");
|
||||
My_Root_Node := DOM.Core.Nodes.Append_Child (My_Document, My_Root_Node);
|
||||
My_Element_Node := DOM.Core.Documents.Create_Element (My_Document, "element");
|
||||
My_Element_Node := DOM.Core.Nodes.Append_Child (My_Root_Node, My_Element_Node);
|
||||
My_Text_Node := DOM.Core.Documents.Create_Text_Node (My_Document, "Some text here");
|
||||
My_Text_Node := DOM.Core.Nodes.Append_Child (My_Element_Node, My_Text_Node);
|
||||
DOM.Core.Nodes.Write
|
||||
(Stream => Ada.Text_IO.Text_Streams.Stream
|
||||
(Ada.Text_IO.Standard_Output),
|
||||
N => My_Document,
|
||||
Pretty_Print => True);
|
||||
end Serialization;
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
version = "1.0"
|
||||
xmlheader := "<?xml version=" . version . "?>" . "<" . root . ">"
|
||||
|
||||
element("root", "child")
|
||||
element("root", "element", "more text here")
|
||||
element("root_child", "kid", "yak yak")
|
||||
MsgBox % xmlheader . serialize("root")
|
||||
Return
|
||||
|
||||
element(parent, name, text="")
|
||||
{
|
||||
Global
|
||||
%parent%_children .= name . "`n"
|
||||
%parent%_%name% = %parent%_%name%
|
||||
%parent%_%name%_name := name
|
||||
%parent%_%name%_text := text
|
||||
}
|
||||
|
||||
serialize(root){
|
||||
StringSplit, root, root, _
|
||||
xml .= "<" . root%root0% . ">"
|
||||
StringTrimRight, %root%_children, %root%_children, 1
|
||||
Loop, Parse, %root%_children, `n
|
||||
{
|
||||
If %root%_%A_LoopField%_children
|
||||
xml .= serialize(%root%_%A_LoopField%)
|
||||
Else
|
||||
{
|
||||
element := "<" . %root%_%A_LoopField%_name . ">"
|
||||
element .= %root%_%A_LoopField%_text
|
||||
element .= "</" . %root%_%A_LoopField%_name . ">"
|
||||
xml .= element
|
||||
}
|
||||
}
|
||||
Return xml .= "</" . root%root0% . ">"
|
||||
}
|
||||
16
Task/XML-DOM-serialization/C-sharp/xml-dom-serialization.cs
Normal file
16
Task/XML-DOM-serialization/C-sharp/xml-dom-serialization.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
[XmlRoot("root")]
|
||||
public class ExampleXML
|
||||
{
|
||||
[XmlElement("element")]
|
||||
public string element = "Some text here";
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var xmlnamespace = new XmlSerializerNamespaces();
|
||||
xmlnamespace.Add("", ""); //used to stop default namespaces from printing
|
||||
var writer = XmlWriter.Create("output.xml");
|
||||
new XmlSerializer(typeof(ExampleXML)).Serialize(writer, new ExampleXML(), xmlnamespace);
|
||||
}
|
||||
//Output: <?xml version="1.0" encoding="utf-8"?><root><element>Some text here</element></root>
|
||||
}
|
||||
29
Task/XML-DOM-serialization/C/xml-dom-serialization.c
Normal file
29
Task/XML-DOM-serialization/C/xml-dom-serialization.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <libxml/parser.h>
|
||||
#include <libxml/tree.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
const char **next;
|
||||
int a;
|
||||
FILE *outFile;
|
||||
|
||||
xmlDoc *doc = xmlNewDoc("1.0");
|
||||
xmlNode *root = xmlNewNode(NULL, "root");
|
||||
xmlDocSetRootElement(doc, root);
|
||||
|
||||
xmlNode *node = xmlNewNode(NULL, "element");
|
||||
xmlAddChild(node, xmlNewText("some text here"));
|
||||
xmlAddChild(root, node);
|
||||
|
||||
outFile = fopen("myfile.xml", "w");
|
||||
xmlElemDump(outFile, doc, root);
|
||||
fclose(outFile);
|
||||
|
||||
xmlFreeDoc(doc);
|
||||
xmlCleanupParser();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(let* ((doc (dom:create-document 'rune-dom:implementation nil nil nil))
|
||||
(root (dom:create-element doc "root"))
|
||||
(element (dom:create-element doc "element"))
|
||||
(text (dom:create-text-node doc "Some text here")))
|
||||
(dom:append-child element text)
|
||||
(dom:append-child root element)
|
||||
(dom:append-child doc root)
|
||||
(dom:map-document (cxml:make-rod-sink) doc))
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(let ((doc (dom:create-document 'rune-dom:implementation nil nil nil)))
|
||||
(dom:append-child
|
||||
(dom:append-child
|
||||
(dom:append-child doc (dom:create-element doc "root"))
|
||||
(dom:create-element doc "element"))
|
||||
(dom:create-text-node doc "Some text here"))
|
||||
(write-string (dom:map-document (cxml:make-rod-sink) doc)))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
(defun append-child* (parent &rest children)
|
||||
(reduce 'dom:append-child children :initial-value parent))
|
||||
|
||||
(let* ((doc (dom:create-document 'rune-dom:implementation nil nil nil)))
|
||||
(append-child* doc
|
||||
(dom:create-element doc "root")
|
||||
(dom:create-element doc "element")
|
||||
(dom:create-text-node doc "Some text here"))
|
||||
(write-string (dom:map-document (cxml:make-rod-sink) doc)))
|
||||
12
Task/XML-DOM-serialization/D/xml-dom-serialization.d
Normal file
12
Task/XML-DOM-serialization/D/xml-dom-serialization.d
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
module xmltest ;
|
||||
|
||||
import std.stdio ;
|
||||
import std.xml ;
|
||||
|
||||
void main() {
|
||||
auto doc = new Document("root") ;
|
||||
//doc.prolog = q"/<?xml version="1.0"?>/" ; // default
|
||||
doc ~= new Element("element", "Some text here") ;
|
||||
writefln(doc) ;
|
||||
// output: <?xml version="1.0"?><root><element>Some text here</element></root>
|
||||
}
|
||||
11
Task/XML-DOM-serialization/E/xml-dom-serialization.e
Normal file
11
Task/XML-DOM-serialization/E/xml-dom-serialization.e
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def document := <unsafe:javax.xml.parsers.makeDocumentBuilderFactory> \
|
||||
.newInstance() \
|
||||
.newDocumentBuilder() \
|
||||
.getDOMImplementation() \
|
||||
.createDocument(null, "root", null)
|
||||
def root := document.getDocumentElement()
|
||||
root.appendChild(
|
||||
def element := document.createElement("element"))
|
||||
element.appendChild(
|
||||
document.createTextNode("Some text here"))
|
||||
println(document.saveXML(root))
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
using xml
|
||||
|
||||
class XmlDom
|
||||
{
|
||||
public static Void main ()
|
||||
{
|
||||
doc := XDoc()
|
||||
root := XElem("root")
|
||||
doc.add (root)
|
||||
|
||||
child := XElem("element")
|
||||
child.add(XText("Some text here"))
|
||||
root.add (child)
|
||||
|
||||
doc.write(Env.cur.out)
|
||||
}
|
||||
}
|
||||
29
Task/XML-DOM-serialization/Forth/xml-dom-serialization.fth
Normal file
29
Task/XML-DOM-serialization/Forth/xml-dom-serialization.fth
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
include ffl/dom.fs
|
||||
|
||||
\ Create a dom variable 'doc' in the dictionary
|
||||
|
||||
dom-create doc
|
||||
|
||||
\ Add the document root with its version attribute
|
||||
|
||||
dom.document doc dom-append-node
|
||||
|
||||
s" version" s" 1.0" dom.attribute doc dom-append-node
|
||||
|
||||
\ Add root and element
|
||||
|
||||
doc dom-parent 2drop
|
||||
|
||||
s" root" dom.element doc dom-append-node
|
||||
|
||||
s" element" dom.element doc dom-append-node
|
||||
|
||||
\ Add the text
|
||||
|
||||
s" Some text here" dom.text doc dom-append-node
|
||||
|
||||
\ Convert the document to a string and print
|
||||
|
||||
doc dom-write-string [IF]
|
||||
type cr
|
||||
[THEN]
|
||||
21
Task/XML-DOM-serialization/Go/xml-dom-serialization-1.go
Normal file
21
Task/XML-DOM-serialization/Go/xml-dom-serialization-1.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
dom "bitbucket.org/rj/xmldom-go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
d, err := dom.ParseStringXml(`
|
||||
<?xml version="1.0" ?>
|
||||
<root>
|
||||
<element>
|
||||
Some text here
|
||||
</element>
|
||||
</root>`)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(d.ToXml()))
|
||||
}
|
||||
5
Task/XML-DOM-serialization/Go/xml-dom-serialization-2.go
Normal file
5
Task/XML-DOM-serialization/Go/xml-dom-serialization-2.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<root>
|
||||
<element>
|
||||
Some text here
|
||||
</element>
|
||||
</root>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import groovy.xml.MarkupBuilder
|
||||
def writer = new StringWriter() << '<?xml version="1.0" ?>\n'
|
||||
def xml = new MarkupBuilder(writer)
|
||||
xml.root() {
|
||||
element('Some text here' )
|
||||
}
|
||||
println writer
|
||||
14
Task/XML-DOM-serialization/Haskell/xml-dom-serialization.hs
Normal file
14
Task/XML-DOM-serialization/Haskell/xml-dom-serialization.hs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import Data.List
|
||||
import Text.XML.Light
|
||||
|
||||
xmlDOM :: String -> String
|
||||
xmlDOM txt = showTopElement $ Element
|
||||
(unqual "root")
|
||||
[]
|
||||
[ Elem $ Element
|
||||
(unqual "element")
|
||||
[]
|
||||
[Text $ CData CDataText txt Nothing]
|
||||
Nothing
|
||||
]
|
||||
Nothing
|
||||
11
Task/XML-DOM-serialization/J/xml-dom-serialization-1.j
Normal file
11
Task/XML-DOM-serialization/J/xml-dom-serialization-1.j
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
serialize=: ('<?xml version="1.0" ?>',LF),;@serialize1&''
|
||||
serialize1=:4 :0
|
||||
if.L.x do.
|
||||
start=. y,'<',(0{::x),'>',LF
|
||||
middle=. ;;(}.x) serialize1&.> <' ',y
|
||||
end=. y,'</',(0{::x),'>',LF
|
||||
<start,middle,end
|
||||
else.
|
||||
<y,x,LF
|
||||
end.
|
||||
)
|
||||
1
Task/XML-DOM-serialization/J/xml-dom-serialization-2.j
Normal file
1
Task/XML-DOM-serialization/J/xml-dom-serialization-2.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
obj=: 'root';<'element';'some text here'
|
||||
7
Task/XML-DOM-serialization/J/xml-dom-serialization-3.j
Normal file
7
Task/XML-DOM-serialization/J/xml-dom-serialization-3.j
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
serialize obj
|
||||
<?xml version="1.0" ?>
|
||||
<root>
|
||||
<element>
|
||||
some text here
|
||||
</element>
|
||||
</root>
|
||||
105
Task/XML-DOM-serialization/Java/xml-dom-serialization-1.java
Normal file
105
Task/XML-DOM-serialization/Java/xml-dom-serialization-1.java
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import java.io.StringWriter;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.w3c.dom.DOMImplementation;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public class RDOMSerialization {
|
||||
|
||||
private Document domDoc;
|
||||
|
||||
public RDOMSerialization() {
|
||||
return;
|
||||
}
|
||||
|
||||
protected void buildDOMDocument() {
|
||||
|
||||
DocumentBuilderFactory factory;
|
||||
DocumentBuilder builder;
|
||||
DOMImplementation impl;
|
||||
Element elmt1;
|
||||
Element elmt2;
|
||||
|
||||
try {
|
||||
factory = DocumentBuilderFactory.newInstance();
|
||||
builder = factory.newDocumentBuilder();
|
||||
impl = builder.getDOMImplementation();
|
||||
domDoc = impl.createDocument(null, null, null);
|
||||
elmt1 = domDoc.createElement("root");
|
||||
elmt2 = domDoc.createElement("element");
|
||||
elmt2.setTextContent("Some text here");
|
||||
|
||||
domDoc.appendChild(elmt1);
|
||||
elmt1.appendChild(elmt2);
|
||||
}
|
||||
catch (ParserConfigurationException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
protected void serializeXML() {
|
||||
|
||||
DOMSource domSrc;
|
||||
Transformer txformer;
|
||||
StringWriter sw;
|
||||
StreamResult sr;
|
||||
|
||||
try {
|
||||
domSrc = new DOMSource(domDoc);
|
||||
|
||||
txformer = TransformerFactory.newInstance().newTransformer();
|
||||
txformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
||||
txformer.setOutputProperty(OutputKeys.METHOD, "xml");
|
||||
txformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||
txformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
txformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
|
||||
txformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||
|
||||
sw = new StringWriter();
|
||||
sr = new StreamResult(sw);
|
||||
|
||||
txformer.transform(domSrc, sr);
|
||||
|
||||
System.out.println(sw.toString());
|
||||
}
|
||||
catch (TransformerConfigurationException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
catch (TransformerFactoryConfigurationError ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
public static void serializationDriver(String[] args) {
|
||||
|
||||
RDOMSerialization lcl = new RDOMSerialization();
|
||||
lcl.buildDOMDocument();
|
||||
lcl.serializeXML();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
serializationDriver(args);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<root>
|
||||
<element>Some text here</element>
|
||||
</root>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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 );
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
var xml = <root>
|
||||
<element>Some text here</element>
|
||||
</root>;
|
||||
var xmlString = xml.toXMLString();
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
XML.ignoreProcessingInstructions = false;
|
||||
var xml = <?xml version="1.0"?>
|
||||
<root>
|
||||
<element>Some text here</element>
|
||||
</root>;
|
||||
var xmlString = xml.toXMLString();
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
DOM = XMLObject["Document"][{XMLObject["Declaration"]["Version" -> "1.0","Encoding" -> "utf-8"]},
|
||||
XMLElement["root", {}, {XMLElement["element", {}, {"Some text here"}]}], {}];
|
||||
|
||||
ExportString[DOM, "XML", "AttributeQuoting" -> "\""]
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
import java.io.StringWriter
|
||||
import javax.xml.
|
||||
import org.w3c.dom.
|
||||
|
||||
class RDOMSerialization public
|
||||
|
||||
properties private
|
||||
domDoc = Document
|
||||
|
||||
method main(args = String[]) public static
|
||||
|
||||
lcl = RDOMSerialization()
|
||||
lcl.buildDOMDocument()
|
||||
lcl.serializeXML()
|
||||
|
||||
return
|
||||
|
||||
method buildDOMDocument() inheritable
|
||||
|
||||
do
|
||||
factory = DocumentBuilderFactory.newInstance()
|
||||
builder = factory.newDocumentBuilder()
|
||||
impl = builder.getDOMImplementation()
|
||||
domDoc = impl.createDocument(null, null, null)
|
||||
elmt1 = domDoc.createElement("root")
|
||||
elmt2 = domDoc.createElement("element")
|
||||
elmt2.setTextContent("Some text here")
|
||||
|
||||
domDoc.appendChild(elmt1)
|
||||
elmt1.appendChild(elmt2)
|
||||
|
||||
catch exPC = ParserConfigurationException
|
||||
exPC.printStackTrace
|
||||
end
|
||||
|
||||
return
|
||||
|
||||
method serializeXML() inheritable
|
||||
|
||||
do
|
||||
domSrc = DOMSource(domDoc)
|
||||
txformer = TransformerFactory.newInstance().newTransformer()
|
||||
txformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no")
|
||||
txformer.setOutputProperty(OutputKeys.METHOD, "xml")
|
||||
txformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")
|
||||
txformer.setOutputProperty(OutputKeys.INDENT, "yes")
|
||||
txformer.setOutputProperty(OutputKeys.STANDALONE, "yes")
|
||||
txformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2")
|
||||
|
||||
sw = StringWriter()
|
||||
sr = StreamResult(sw)
|
||||
|
||||
txformer.transform(domSrc, sr)
|
||||
|
||||
say sw.toString
|
||||
|
||||
catch exTC = TransformerConfigurationException
|
||||
exTC.printStackTrace
|
||||
catch exTF = TransformerFactoryConfigurationError
|
||||
exTF.printStackTrace
|
||||
catch exTE = TransformerException
|
||||
exTE.printStackTrace
|
||||
end
|
||||
|
||||
return
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<root>
|
||||
<element>Some text here</element>
|
||||
</root>
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
use XML;
|
||||
|
||||
bundle Default {
|
||||
class Test {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
builder := XMLBuilder->New("root", "1.0");
|
||||
root := builder->GetRoot();
|
||||
element := XMLElement->New(XMLElementType->ELEMENT, "element", "Some text here");
|
||||
root->AddChild(element);
|
||||
builder->ToString()->PrintLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
DEFINE VARIABLE hxdoc AS HANDLE NO-UNDO.
|
||||
DEFINE VARIABLE hxroot AS HANDLE NO-UNDO.
|
||||
DEFINE VARIABLE hxelement AS HANDLE NO-UNDO.
|
||||
DEFINE VARIABLE hxtext AS HANDLE NO-UNDO.
|
||||
DEFINE VARIABLE lcc AS LONGCHAR NO-UNDO.
|
||||
|
||||
CREATE X-DOCUMENT hxdoc.
|
||||
|
||||
CREATE X-NODEREF hxroot.
|
||||
hxdoc:CREATE-NODE( hxroot, 'root', 'ELEMENT' ).
|
||||
hxdoc:APPEND-CHILD( hxroot ).
|
||||
|
||||
CREATE X-NODEREF hxelement.
|
||||
hxdoc:CREATE-NODE( hxelement, 'element', 'ELEMENT' ).
|
||||
hxroot:APPEND-CHILD( hxelement ).
|
||||
|
||||
CREATE X-NODEREF hxtext.
|
||||
hxdoc:CREATE-NODE( hxtext, 'element', 'TEXT' ).
|
||||
hxelement:APPEND-CHILD( hxtext ).
|
||||
hxtext:NODE-VALUE = 'Some text here'.
|
||||
|
||||
hxdoc:SAVE( 'LONGCHAR', lcc ).
|
||||
MESSAGE STRING( lcc ) VIEW-AS ALERT-BOX.
|
||||
7
Task/XML-DOM-serialization/Oz/xml-dom-serialization-1.oz
Normal file
7
Task/XML-DOM-serialization/Oz/xml-dom-serialization-1.oz
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
declare
|
||||
proc {Main}
|
||||
DOM = root(element("Some text here"))
|
||||
in
|
||||
{System.showInfo {Serialize DOM}}
|
||||
end
|
||||
...
|
||||
4
Task/XML-DOM-serialization/Oz/xml-dom-serialization-2.oz
Normal file
4
Task/XML-DOM-serialization/Oz/xml-dom-serialization-2.oz
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" ?>
|
||||
<root>
|
||||
<element>Some text here</element>
|
||||
</root>
|
||||
9
Task/XML-DOM-serialization/PHP/xml-dom-serialization.php
Normal file
9
Task/XML-DOM-serialization/PHP/xml-dom-serialization.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?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
|
||||
$root = $dom->createElement('root');
|
||||
$element = $dom->createElement('element');
|
||||
$element->appendChild($dom->createTextNode('Some text here'));
|
||||
$root->appendChild($element);
|
||||
$dom->appendChild($root);
|
||||
$xmlstring = $dom->saveXML();
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
program CrearXML;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
uses
|
||||
Classes, XMLWrite, DOM;
|
||||
|
||||
var
|
||||
xdoc: TXMLDocument; // variable objeto documento XML
|
||||
NodoRaiz, NodoPadre, NodoHijo: TDOMNode; // variables a los nodos
|
||||
begin
|
||||
//crear el documento
|
||||
xdoc := TXMLDocument.create;
|
||||
|
||||
NodoRaiz := xdoc.CreateElement('root'); // crear el nodo raíz
|
||||
Xdoc.Appendchild(NodoRaiz); // guardar nodo raíz
|
||||
NodoPadre := xdoc.CreateElement('element'); // crear el nodo hijo
|
||||
NodoHijo := xdoc.CreateTextNode('Some text here'); // insertar el valor del nodo
|
||||
NodoPadre.Appendchild(NodoHijo); // guardar nodo
|
||||
NodoRaiz.AppendChild(NodoPadre); // insertar el nodo hijo en el correspondiente nodo padre
|
||||
writeXMLFile(xDoc,'prueba.xml'); // escribir el XML
|
||||
Xdoc.free;
|
||||
end.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
use XML::Simple;
|
||||
print XMLout( { root => { element => "Some text here" } }, NoAttr => 1, RootName => "" );
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
use XML::DOM::BagOfTricks qw(createDocument createTextElement);
|
||||
|
||||
my ($doc, $root) = createDocument('root');
|
||||
$root->appendChild(
|
||||
createTextElement($doc, 'element', 'Some text here')
|
||||
);
|
||||
print $doc->toString;
|
||||
12
Task/XML-DOM-serialization/Perl/xml-dom-serialization-3.pl
Normal file
12
Task/XML-DOM-serialization/Perl/xml-dom-serialization-3.pl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
use XML::LibXML;
|
||||
|
||||
$xml = XML::LibXML::Document->new('1.0');
|
||||
$node = $xml->createElement('root');
|
||||
$xml->setDocumentElement($node);
|
||||
$node2 = $xml->createElement('element');
|
||||
$text = $xml->createTextNode('Some text here');
|
||||
$node2->addChild($text);
|
||||
$node->appendWellBalancedChunk('text');
|
||||
$node->addChild($node2);
|
||||
|
||||
print $xml->toString;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(load "@lib/xm.l")
|
||||
|
||||
(xml? T)
|
||||
(xml '(root NIL (element NIL "Some text here")))
|
||||
14
Task/XML-DOM-serialization/Pike/xml-dom-serialization-1.pike
Normal file
14
Task/XML-DOM-serialization/Pike/xml-dom-serialization-1.pike
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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", ([]), "");
|
||||
dom->add_child(node);
|
||||
|
||||
object subnode = Parser.XML.Tree.SimpleNode(Parser.XML.Tree.XML_ELEMENT, "element", ([]), "");
|
||||
node->add_child(subnode);
|
||||
|
||||
node = subnode;
|
||||
subnode = Parser.XML.Tree.SimpleNode(Parser.XML.Tree.XML_TEXT, "", ([]), "Some text here");
|
||||
node->add_child(subnode);
|
||||
|
||||
dom->render_xml();
|
||||
Result: "<?xml version='1.0' encoding='utf-8'?><root><element>Some text here</element></root>"
|
||||
27
Task/XML-DOM-serialization/Pike/xml-dom-serialization-2.pike
Normal file
27
Task/XML-DOM-serialization/Pike/xml-dom-serialization-2.pike
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
object make_xml_node(array|string in, void|int level)
|
||||
{
|
||||
level++;
|
||||
if (stringp(in))
|
||||
return Parser.XML.Tree.SimpleNode(Parser.XML.Tree.XML_TEXT, "", ([]), in);
|
||||
else
|
||||
{
|
||||
object node = Parser.XML.Tree.SimpleNode(Parser.XML.Tree.XML_ELEMENT, in[0], in[1], "");
|
||||
foreach(in[2..];; array|string child)
|
||||
{
|
||||
node->add_child(make_xml_node(child, level));
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
object make_xml_tree(array input)
|
||||
{
|
||||
object dom = Parser.XML.Tree.SimpleRootNode();
|
||||
dom->add_child(Parser.XML.Tree.SimpleNode(Parser.XML.Tree.XML_HEADER, "", ([]), ""));
|
||||
dom->add_child(make_xml_node(input));
|
||||
return dom;
|
||||
}
|
||||
|
||||
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>"
|
||||
32
Task/XML-DOM-serialization/Pike/xml-dom-serialization-3.pike
Normal file
32
Task/XML-DOM-serialization/Pike/xml-dom-serialization-3.pike
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
object indent_xml(object parent, void|int indent_text, void|int level)
|
||||
{
|
||||
int subnodes = false;
|
||||
foreach(parent->get_children();; object child)
|
||||
{
|
||||
if (child->get_node_type() == Parser.XML.Tree.XML_ELEMENT ||
|
||||
(child->get_node_type() == Parser.XML.Tree.XML_TEXT && indent_text))
|
||||
{
|
||||
subnodes = true;
|
||||
parent->add_child_before(Parser.XML.Tree.SimpleNode(Parser.XML.Tree.XML_TEXT, "", ([]), "\r\n"+" "*level), child);
|
||||
indent_xml(child, indent_text, level+1);
|
||||
}
|
||||
}
|
||||
if (subnodes && level)
|
||||
parent->add_child(Parser.XML.Tree.SimpleNode(Parser.XML.Tree.XML_TEXT, "", ([]), "\r\n"+" "*(level-1)));
|
||||
return parent;
|
||||
}
|
||||
|
||||
|
||||
indent_xml(make_xml_tree(input))->render_xml();
|
||||
Result: "<?xml version='1.0' encoding='utf-8'?>\r\n"
|
||||
"<root>\r\n"
|
||||
" <element>Some text here</element>\r\n"
|
||||
"</root>"
|
||||
|
||||
indent_xml(make_xml_tree(input), 1)->render_xml();
|
||||
Result: "<?xml version='1.0' encoding='utf-8'?>\r\n"
|
||||
"<root>\r\n"
|
||||
" <element>\r\n"
|
||||
" Some text here\r\n"
|
||||
" </element>\r\n"
|
||||
"</root>"
|
||||
12
Task/XML-DOM-serialization/Python/xml-dom-serialization-1.py
Normal file
12
Task/XML-DOM-serialization/Python/xml-dom-serialization-1.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
from xml.dom.minidom import getDOMImplementation
|
||||
|
||||
dom = getDOMImplementation()
|
||||
document = dom.createDocument(None, "root", None)
|
||||
|
||||
topElement = document.documentElement
|
||||
firstElement = document.createElement("element")
|
||||
topElement.appendChild(firstElement)
|
||||
textNode = document.createTextNode("Some text here")
|
||||
firstElement.appendChild(textNode)
|
||||
|
||||
xmlString = document.toprettyxml(" " * 4)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
from xml.etree import ElementTree as et
|
||||
|
||||
root = et.Element("root")
|
||||
et.SubElement(root, "element").text = "Some text here"
|
||||
xmlString = et.tostring(root)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import lang::xml::DOM;
|
||||
|
||||
public void main(){
|
||||
x = document(element(none(), "root", [element(none(), "element", [charData("Some text here")])]));
|
||||
return println(xmlPretty(x));
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
rascal>main()
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<root>
|
||||
<element>Some text here</element>
|
||||
</root>
|
||||
12
Task/XML-DOM-serialization/Ruby/xml-dom-serialization.rb
Normal file
12
Task/XML-DOM-serialization/Ruby/xml-dom-serialization.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
require("rexml/document")
|
||||
include REXML
|
||||
(doc = Document.new) << XMLDecl.new
|
||||
root = doc.add_element('root')
|
||||
element = root.add_element('element')
|
||||
element.add_text('Some text here')
|
||||
|
||||
# save to a string
|
||||
# (the first argument to write() needs an object that understands "<<")
|
||||
serialized = String.new
|
||||
doc.write(serialized, 4)
|
||||
puts serialized
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
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)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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}
|
||||
13
Task/XML-DOM-serialization/XProc/xml-dom-serialization.xproc
Normal file
13
Task/XML-DOM-serialization/XProc/xml-dom-serialization.xproc
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<p:pipeline xmlns:p="http://www.w3.org/ns/xproc" version="1.0">
|
||||
<p:identity>
|
||||
<p:input port="source">
|
||||
<p:inline>
|
||||
<root>
|
||||
<element>
|
||||
Some text here
|
||||
</element>
|
||||
</root>
|
||||
</p:inline>
|
||||
</p:input>
|
||||
</p:identity>
|
||||
</p:pipeline>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<root>
|
||||
<element>
|
||||
Some text here
|
||||
</element>
|
||||
</root>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
let $rootTagname := 'root'
|
||||
let $elementTagname := 'element'
|
||||
let $elementContent := 'Some text here'
|
||||
|
||||
return
|
||||
element {$rootTagname}
|
||||
{
|
||||
element{$elementTagname}
|
||||
{$elementContent}
|
||||
}
|
||||
10
Task/XML-DOM-serialization/XSLT/xml-dom-serialization.xslt
Normal file
10
Task/XML-DOM-serialization/XSLT/xml-dom-serialization.xslt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<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 -->
|
||||
<xsl:element name="root">
|
||||
<xsl:element name="element">
|
||||
<xsl:text>Some text here</xsl:text>
|
||||
</xsl:element>
|
||||
</xsl:element>
|
||||
</xsl:template>
|
||||
</xsl:stylesheet>
|
||||
Loading…
Add table
Add a link
Reference in a new issue