XML/DOM serialization: Difference between revisions

From Rosetta Code
< XML
Content added Content deleted
mNo edit summary
Line 1: Line 1:
{{task}}
[http://zodip.info/description/o-pson.htm o pson e cas] [http://puli.org.cn/web/catania-erboristerie/ catania erboristerie] [http://zodip.info/description/mia-moglie.htm mia moglie personali] [http://puli.org.cn/web/horologium/ horologium] [http://nibko.info/data/hp-ipaq.htm hp- ipaq hw6515] [http://italnet.net.cn/img/tre-amici/ tre amici] [http://puli.org.cn/web/bdsm-pics/ bdsm pics] [http://puli.org.cn/web/mp-gb/ mp3 1gb odys s9] [http://hangzhau.cn/html/can-t/ can t wait until tonight baby] [http://puli.org.cn/web/got-you/ got you] [http://zodip.info/description/aspetti-legali.htm aspetti legali associazione no profit] [http://zodip.info/description/immagini-calendari.htm immagini calendari2005] [http://nibko.info/data/adidas-scarpe.htm adidas scarpe] [http://zodip.info/description/il-padrino.htm il padrino dvd] [http://hangzhau.cn/html/valzer-cuore/ valzer cuore di mamma] [http://nibko.info/data/carlo-scarpa.htm carlo scarpa] [http://zodip.info/description/le-ragazze.htm le ragazze di piazza di spagna] [http://italnet.net.cn/img/hotel-vila/ hotel vila gale salvador] [http://nibko.info/data/don-t.htm don t let me get me pink] [http://puli.org.cn/web/malco/ malco] [http://zodip.info/description/testo-di.htm testo di anastacia left outside alone] [http://zodip.info/description/mp-lettori.htm mp3 lettori sd] [http://zodip.info/description/fotocopiatrice-da.htm fotocopiatrice da tavolo] [http://puli.org.cn/web/celebrity-fight/ celebrity fight club] [http://nibko.info/data/kaspersky-.htm kaspersky anti-virus avp] [http://italnet.net.cn/img/sistema-babilonico/ sistema babilonico] [http://zodip.info/description/francesca-lancini.htm francesca lancini] [http://zodip.info/description/o-zones.htm o zones despre tine] [http://puli.org.cn/web/cartier-per/ cartier per uomo declaration] [http://puli.org.cn/web/bebe-confort/ bebe confort elios] [http://nibko.info/data/mixer-behringer.htm mixer behringer eurorack] [http://zodip.info/description/dragoste-di.htm dragoste di tei] [http://italnet.net.cn/img/tony-presidio/ tony presidio] [http://puli.org.cn/web/lavastoviglie/ lavastoviglie] [http://puli.org.cn/web/mld-see/ ml-2250d5 see samsung] [http://nibko.info/data/masterizzatore-lg.htm masterizzatore lg 4163] [http://puli.org.cn/web/shoking-blue/ shoking blue] [http://hangzhau.cn/html/agassi-tennis/ agassi tennis generation] [http://zodip.info/description/superlinko.htm superlinko] [http://hangzhau.cn/html/clasificacion-de/ clasificacion de sectores social] [http://hangzhau.cn/html/testi-salsa/ testi salsa] [http://puli.org.cn/web/siepi/ siepi] [http://hangzhau.cn/html/imbarcazioni-diporto/ imbarcazioni diporto] [http://nibko.info/data/tastiera-pc.htm tastiera pc wireless] [http://italnet.net.cn/img/le-ali/ le ali della liberta film] [http://nibko.info/data/lettore-mp.htm lettore mp3 packard] [http://italnet.net.cn/img/resistencia-a/ resistencia a la torsion] [http://puli.org.cn/web/pedagogia-operatoria/ pedagogia operatoria] [http://italnet.net.cn/img/opel-astra/ opel astra 1 7 dti sw] [http://hangzhau.cn/html/telecamere-digitale/ telecamere digitale con dvd] [http://italnet.net.cn/img/camera-eindhoven/ camera eindhoven] [http://hangzhau.cn/html/lets-outside/ lets outside alone] [http://puli.org.cn/web/casa-ecologica/ casa ecologica] [http://italnet.net.cn/img/program-/ program 6 cpr 409,8 mg] [http://zodip.info/description/giochi-flash.htm giochi flash tutto gratis] [http://puli.org.cn/web/vrx/ vrx 848] [http://italnet.net.cn/img/hp-laserjet/ hp laserjet 1000 toner] [http://zodip.info/description/matinata.htm matinata] [http://hangzhau.cn/html/luce-lettura/ luce lettura] [http://puli.org.cn/web/danza-di/ danza di esmeralda] [http://italnet.net.cn/img/hot-tennis/ hot tennis] [http://zodip.info/description/www-cupramarittima.htm www cupramarittima it] [http://italnet.net.cn/img/safri-duo/ safri duo rise mp3] [http://italnet.net.cn/img/unita-di/ unita di misura scarpe] [http://puli.org.cn/web/novita-decoder/ novita decoder] [http://puli.org.cn/web/accessori-palmare/ accessori palmare hp] [http://nibko.info/data/borsa-a.htm borsa a tracolla uomo] [http://puli.org.cn/web/car-audio/ car audio mp3] [http://italnet.net.cn/img/noleggio-a/ noleggio a lungo termine bmw x5] [http://nibko.info/data/panasonic-gs.htm panasonic gs-250] [http://puli.org.cn/web/cumbia-midi/ cumbia midi] [http://nibko.info/data/last-minute.htm last minute dolomiti] [http://puli.org.cn/web/benny-benassi/ benny benassi ft dhany] [http://nibko.info/data/aspido.htm aspido-] [http://nibko.info/data/eleganti.htm eleganti] [http://hangzhau.cn/html/benny-benassi/ benny benassi techno cocaine] [http://puli.org.cn/web/barbie-moda/ barbie moda] [http://italnet.net.cn/img/modem-telecom/ modem telecom] [http://italnet.net.cn/img/abbigliamento-les/ abbigliamento les petits] [http://hangzhau.cn/html/giochi-di/ giochi di elicotteri] [http://zodip.info/description/grandes-esperanzas.htm grandes esperanzas] [http://italnet.net.cn/img/temi-italiano/ temi italiano esami di maturita 2004] [http://nibko.info/data/stampanti-hp.htm stampanti hp designjet 4000] [http://hangzhau.cn/html/chanlin/ chanlin] [http://nibko.info/data/di-garfild.htm di garfild] [http://hangzhau.cn/html/colonnine/ colonnine] [http://italnet.net.cn/img/trimone/ trimone] [http://puli.org.cn/web/starmania/ starmania] [http://hangzhau.cn/html/palmari-toshiba/ palmari toshiba pocket pc e740] [http://hangzhau.cn/html/il-maresciallo/ il maresciallo rocca. stagione 3. episodio 3 - 4] [http://zodip.info/description/palazzetti.htm palazzetti] [http://nibko.info/data/milupa-latte.htm milupa latte] [http://zodip.info/description/satkeys.htm satkeys] [http://puli.org.cn/web/cina/ cina] [http://nibko.info/data/antivirus-norton.htm antivirus norton internet security] [http://zodip.info/description/samsung-wide.htm samsung wide] [http://puli.org.cn/web/copie-donne/ copie donne mature] [http://puli.org.cn/web/noleggio-hyundai/ noleggio hyundai tucson] [http://nibko.info/data/ballata-per.htm ballata per un re] [http://hangzhau.cn/html/uomini-anni/ uomini, anni, vita] [http://nibko.info/data/giochi-da.htm giochi da bambini] [http://puli.org.cn/web/torino-gay/ torino gay escort] [http://hangzhau.cn/html/lyric-tragedie/ lyric tragedie] [http://nibko.info/data/ginnastica-dimagrire.htm ginnastica dimagrire] [http://puli.org.cn/web/bruno-asisi/ bruno asisi] [http://hangzhau.cn/html/video-sexy/ video sexy gratis on line] [http://puli.org.cn/web/duke-of/ duke of clarence] [http://hangzhau.cn/html/katherine-mansfield/ katherine mansfield] [http://italnet.net.cn/img/fernando-sor/ fernando sor mozart] [http://puli.org.cn/web/divina-commedia/ divina commedia parafrasi purgatorio] [http://italnet.net.cn/img/legendofzelda/ legendofzelda] [http://italnet.net.cn/img/antenna-satellitare/ antenna satellitare] [http://hangzhau.cn/html/desktop-asus/ desktop asus] [http://zodip.info/description/responsabile-della.htm responsabile della manutenzione (regione lombardia - milano provincia)] [http://italnet.net.cn/img/hard-disk/ hard disk esterno sitecom 200] [http://nibko.info/data/www-reintegrazione.htm www reintegrazione it] [http://nibko.info/data/video-americano.htm video americano iraq] [http://hangzhau.cn/html/f-kit/ f kit eamon] [http://nibko.info/data/www-angolo.htm www angolo testi it titolo canzone non m] [http://zodip.info/description/megaimage-x.htm mega-image 67x] [http://hangzhau.cn/html/gold-avenue/ gold avenue] [http://zodip.info/description/adattatore-pad.htm adattatore pad xbox per pc] [http://puli.org.cn/web/www-juan/ www juan pablo raba com] [http://puli.org.cn/web/www-costa/ www costa crociere it offerte] [http://puli.org.cn/web/claiv/ claiv] [http://italnet.net.cn/img/gps-geonav/ gps geonav 5] [http://puli.org.cn/web/guida-donda/ guida donda] [http://zodip.info/description/xerox-docuprint.htm xerox docuprint m750] [http://italnet.net.cn/img/dds-sony/ dds sony] [http://nibko.info/data/marce-militari.htm marce militari napoleone] [http://zodip.info/description/strange-invaders.htm strange invaders] [http://hangzhau.cn/html/drin-drin/ drin drin 1] [http://nibko.info/data/asics-mexico.htm asics mexico] [http://italnet.net.cn/img/download-la/ download la serenissima] [http://hangzhau.cn/html/camera-marseille/ camera marseille] [http://puli.org.cn/web/piastre-in/ piastre in ghisa per cucine] [http://nibko.info/data/carisoprodol-online.htm carisoprodol online] [http://italnet.net.cn/img/viaggi-columbus/ viaggi columbus] [http://zodip.info/description/slave.htm slave] [http://italnet.net.cn/img/le-avventure/ le avventure di scooby doo] [http://italnet.net.cn/img/stampanti-laser/ stampanti laser nt] [http://puli.org.cn/web/scuola-di/ scuola di canto] [http://nibko.info/data/hd-esterno.htm hd esterno 120 gb] [http://nibko.info/data/madaa.htm madaa] [http://zodip.info/description/los-fronterizos.htm los fronterizos] [http://zodip.info/description/bmw-veneto.htm bmw veneto] [http://puli.org.cn/web/so-happy/ so happy togther] [http://nibko.info/data/cavalieri-selvaggi.htm cavalieri selvaggi] [http://hangzhau.cn/html/donau/ donau] [http://zodip.info/description/promozione-on.htm promozione on line] [http://hangzhau.cn/html/tenero/ tenero] [http://nibko.info/data/scampanio.htm scampanio] [http://hangzhau.cn/html/adoption-agency/ adoption agency] [http://puli.org.cn/web/eas-myoplex/ eas myoplex carb sense] [http://zodip.info/description/registratore-video.htm registratore video] [http://hangzhau.cn/html/telefoni-cordless/ telefoni cordless master] [http://italnet.net.cn/img/tom-tom/ tom tom per nokia 9500] [http://zodip.info/description/ncaa-gamebreaker.htm ncaa gamebreaker 2001] [http://italnet.net.cn/img/get-it/ get it wile its hot] [http://hangzhau.cn/html/viedma/ viedma] [http://italnet.net.cn/img/kiss-dp/ kiss dp1500] [http://nibko.info/data/risotti.htm risotti] [http://nibko.info/data/bad-compani.htm bad compani] [http://italnet.net.cn/img/contedracula/ contedracula] [http://hangzhau.cn/html/juan-gabriel/ juan gabriel] [http://puli.org.cn/web/ante-legno/ ante legno] [http://zodip.info/description/enigmistica-facile.htm enigmistica facile] [http://hangzhau.cn/html/www-nerogiardini/ www nerogiardini it] [http://zodip.info/description/vanni-de.htm vanni de simone] [http://nibko.info/data/frenata-e.htm frenata e velocita] [http://nibko.info/data/angelia.htm angelia] [http://zodip.info/description/un-freno.htm un freno a disco] [http://puli.org.cn/web/beyond-good/ beyond good evil crack] [http://italnet.net.cn/img/pianto/ pianto] [http://nibko.info/data/more-tv.htm more tv ati] [http://italnet.net.cn/img/visual-basic/ visual basic 6 0] [http://italnet.net.cn/img/centro-sportivo/ centro sportivo] [http://zodip.info/description/in-ginochio.htm in ginochio ritornero da te] [http://zodip.info/description/good-bye.htm good bye lenin soundtrack] [http://zodip.info/description/potocki-jan.htm potocki, jan] [http://puli.org.cn/web/bike-trial/ bike trial] [http://nibko.info/data/cuffia-telefono.htm cuffia telefono fisso] [http://italnet.net.cn/img/mutuo-informazioni/ mutuo informazioni] [http://zodip.info/description/forza-motorsport.htm forza motorsport modificato in immagini] [http://puli.org.cn/web/ma-ia/ ma ia] [http://hangzhau.cn/html/concorso-per/ concorso per 5 vigili urbani] [http://nibko.info/data/a-casa.htm a casa of crabs] [http://puli.org.cn/web/my-friend/ my friend s hot mom] [http://zodip.info/description/catania-corsaro.htm catania corsaro arredo bagno] [http://nibko.info/data/musica-chilena.htm musica chilena] [http://puli.org.cn/web/lg-digitale/ lg digitale terrestre] [http://puli.org.cn/web/epson-stylus/ epson stylus color 440] [http://nibko.info/data/fotos-di.htm fotos di donas colombianas] [http://hangzhau.cn/html/brani-resta/ brani resta in ascolto] [http://nibko.info/data/sfondi-free.htm sfondi free] [http://puli.org.cn/web/norden/ norden] [http://italnet.net.cn/img/paulo-meneguzzi/ paulo meneguzzi] [http://hangzhau.cn/html/bohemian-like/ bohemian like you] [http://puli.org.cn/web/hostel-ibiza/ hostel ibiza] [http://zodip.info/description/traghetti-napoli.htm traghetti napoli tunisi] {{task}}


Create a simple DOM and having it serialize to:
Create a simple DOM and having it serialize to:
Line 29: Line 29:
println(document.saveXML(root))
println(document.saveXML(root))


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

==[[JavaScript]]==
[[Category: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]]==
[[Category: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]]==
[[Category: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]]==
[[Category: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]]==
[[Category: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)

Revision as of 06:14, 2 September 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)