XML/DOM serialization

From Rosetta Code
< XML
Revision as of 20:07, 4 May 2007 by 83.211.3.16 (talk)

free motorola ringtones buy zanaflex cheap flexeril free samsung ringtones free nokia ringtones cheap diazepam sonyericsson ringtones lorazepam online free real ringtones cheap rivotril buy ambien albuterol online buy cialis buy ambien sagem ringtones cheap norco meridia online cheap nexium free mono ringtones cheap hgh cheap ultram cheap ativan buy wellbutrin free ringtones diazepam online sprint ringtones buy lortab carisoprodol online free verizon ringtones buy clomid cheap xenical xanax online free free ringtones wellbutrin online buy carisoprodol free tracfone ringtones sharp ringtones buy ativan buy norco free sony ericsson ringtones free tracfone ringtones sonyericsson ringtones cheap soma order meridia buy zyban online clonazepam free music ringtones cheap cyclobenzaprine ativan online cheap paxil ativan online nexium mp3 ringtones hgh online buy propecia free samsung ringtones ortho zyban free punk ringtones order lipitor mtv ringtones cheap vigrx lorazepam online fioricet online order lorazepam cool ringtones free qwest ringtones cheap diazepam tenuate online cheap clonazepam fioricet online ativan online clonazepam online propecia online free mp3 ringtones hydrocodone online cheap carisoprodol didrex online levitra free nextel ringtones cheap zyban free mp3 ringtones levitra buy celexa tracfone ringtones adipex online free ringtones music ringtones free nextel ringtones free nokia ringtones cheap levitra free ringtones cheap propecia nexium online cialis buy soma mp3 ringtones cheap tramadol cheap phentermine free funny ringtones nexium online wellbutrin online adipex online cheap cialis cheap ultram free alltel ringtones funny ringtones funny ringtones celexa online free real ringtones free free ringtones samsung ringtones cheap viagra cheap diazepam cheap ultram cheap vicodin buy alprazolam cheap propecia diazepam online free mp3 ringtones meridia online meridia online free nokia ringtones buy adipex cheap xenical online hydrocodone cingular ringtones free sagem ringtones buy xanax cheap lisinopril free funny ringtones valium online tracfone ringtones free samsung ringtones ativan online free kyocera ringtones online xanax free mp3 ringtones tramadol online free motorola ringtones free samsung ringtones buy adipex xenical online cheap pharmacy online free real ringtones cheap vicodin ultram online didrex online valium cialis online cheap phentermine prozac online free cingular ringtones free mono ringtones alprazolam online hoodia online cheap xanax free polyphonic ringtones lorazepam online free qwest ringtones diethylpropion online cheap didrex but norco cheap viagra wellbutrin online free wwe ringtones cheap lisinopril free mono ringtones nokia ringtones free alltel ringtones free music ringtones free real ringtones cheap tramadol ambien nokia ringtones qwest ringtones fioricet lorazepam online cheap diazepam free cool ringtones meridia online free nextel ringtones propecia online buy carisoprodol wellbutrin online free tracfone ringtones sprint ringtones free verizon ringtones cheap fioricet didrex online buy alprazolam cyclobenzaprine online free nextel ringtones cheap carisoprodol free nextel ringtones cheap ultram buy valium free sony ringtones cheap fioricet cheap nexium

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>

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

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)

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)