XML/DOM serialization

From Rosetta Code
< XML
Revision as of 06:31, 5 February 2008 by MikeMol (talk | contribs) (Using Template:Libheader, instead of Template:Library)
Task
XML/DOM serialization
You are encouraged to solve this task according to the task description, using any language you may know.

Create a simple DOM and having it serialize to:

 <?xml version="1.0" ?>
 <root>
     <element>
         Some text here
     </element>
 </root>

E

Implementation: E-on-Java

This makes use of XML libraries provided with Java.

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))

(On the use of <unsafe>: The class has not yet been reviewed for E safety, so <import:...makeDocumentBuilderFactory> is not yet allowed. The review would probably be straightforward.)

JavaScript

Interpreter: Firefox 2.0

DOM

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 );

E4X

 var xml = <root>
   <element>Some text here</element>
 </root>;
 var xmlString = xml.toXMLString();

E4X — with processing instruction

 XML.ignoreProcessingInstructions = false;
 var xml = <?xml version="1.0"?>  
 <root>
   <element>Some text here</element>
 </root>;
 var xmlString = xml.toXMLString();

Perl

use XML::DOM::BagOfTricks qw(createDocument createTextElement);

my ($doc, $root) = createDocument('root');
$root->appendChild(
    createTextElement($doc, 'element', 'Some text here')
);
print $doc->toString;

Output:
<root><element>Some text here</element></root>
Library: LibXML
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;

Output:
<?xml version="1.0"?>
<root>text<element>Some text here</element></root>

PHP

Interpreter: PHP 5

 <?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();
 

Python

Interpreter: Python 2.5

 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)


Interpreter: Python 2.5

 from xml.etree import ElementTree as et
 
 root = et.Element("root")
 et.SubElement(root, "element").text = "Some text here"
 xmlString = et.tostring(root)

Ruby

 require("rexml/document")
 include REXML
 (xml = Document.new) << XMLDecl.new
 root=xml.add_element('root')
 element=root.add_element('element')
 element.add_text('Some text here')
 puts xml.to_s(0)

XSLT

<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>