XML/DOM serialization: Difference between revisions

From Rosetta Code
< XML
Content added Content deleted
m (Switch to header template)
Line 10: Line 10:
</root>
</root>


==[[E]]==
=={{header|E}}==
[[Category:E]]

'''Implementation:''' E-on-Java
'''Implementation:''' E-on-Java


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


==[[JavaScript]]==
=={{header|JavaScript}}==
[[Category:JavaScript]]

'''Interpreter:''' Firefox 2.0
'''Interpreter:''' Firefox 2.0


Line 61: Line 57:
var xmlString = xml.toXMLString();
var xmlString = xml.toXMLString();


==[[Perl]]==
=={{header|Perl}}==
[[Category:Perl]]
use XML::DOM::BagOfTricks qw(createDocument createTextElement);
use XML::DOM::BagOfTricks qw(createDocument createTextElement);
Line 71: Line 66:
print $doc->toString;
print $doc->toString;


==[[PHP]]==
=={{header|PHP}}==
[[Category:PHP]]

'''Interpreter:''' [[PHP]] 5
'''Interpreter:''' [[PHP]] 5


Line 87: Line 80:


==[[Python]]==
=={{header|Python}}==
[[Category:Python]]

'''Interpreter:''' [[Python]] 2.5
'''Interpreter:''' [[Python]] 2.5


Line 114: Line 105:
xmlString = et.tostring(root)
xmlString = et.tostring(root)


==[[Ruby]]==
=={{header|Ruby}}==
[[Category:Ruby]]
require("rexml/document")
require("rexml/document")
include REXML
include REXML

Revision as of 05:55, 13 November 2007

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;

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)