XML/DOM serialization: Difference between revisions
< XML
Content deleted Content added
mNo edit summary |
Undo revision 7229 by Special:Contributions/Cf2Nem (User talk:Cf2Nem) |
||
Line 1: | Line 1: | ||
{{task}} |
|||
[http://smegmacheez.ifrance.com/content/view/favara/ favara] [http://joshuaw-wise.ifrance.com/img/styles/vacanze-sicilia/ vacanze sicilia] [http://nokros.ifrance.com/data/dvd-batman/ dvd batman begins] [http://horcrionebay.ifrance.com/resources/articles/agenzia-viaggio.htm agenzia viaggio online] [http://gabe95.ifrance.com/html/video-gloria/ video gloria] [http://call-shotgun.ifrance.com/view/new/verna/ verna] [http://nokros.ifrance.com/data/video-de/ video de lesbianas] [http://smegmacheez.ifrance.com/content/view/www-regione/ www regione sicilia it] [http://gajar.ifrance.com/html/tesmed-elettrostimolatore.htm tesmed elettrostimolatore] [http://mfirishka.ifrance.com/view/new/pc-con.htm pc con media center] [http://gabe95.ifrance.com/html/neil-young/ neil young crazy horse-rust] [http://cocopuff66.ifrance.com/text/directory/stampante-stylus/ stampante stylus photo r200] [http://gabe95.ifrance.com/html/batteria-t/ batteria t310] [http://nokros.ifrance.com/data/htpp-www/ htpp www ata it] [http://atrayah.ifrance.com/topic/in-fondo.htm in fondo al mar mp3] [http://sm4.ifrance.com/lib/enaip-sardegna/ enaip sardegna] [http://flawedamythyst.ifrance.com/img/styles/perego.htm perego] [http://flawedamythyst.ifrance.com/img/styles/libri-scaricabili.htm libri scaricabili] [http://joshuaw-wise.ifrance.com/img/styles/la-casa/ la casa dalle finestre] [http://alishca-st.ifrance.com/text/directory/creative-itrigue.htm creative i-trigue 2 1 3200] [http://call-shotgun.ifrance.com/view/new/quella-sporca/ quella sporca ultima meta] [http://flawedamythyst.ifrance.com/img/styles/colucci.htm colucci] [http://mfirishka.ifrance.com/view/new/corsair-value.htm corsair value select 512 3200] [http://lightmyfire0214.ifrance.com/topic/voli-in/ voli in italia] [http://gabe95.ifrance.com/html/video-scandalo/ video scandalo di mutu] [http://lightmyfire0214.ifrance.com/topic/adattatore-audio/ adattatore audio] [http://call-shotgun.ifrance.com/view/new/kevinlittle/ kevinlittle] [http://flawedamythyst.ifrance.com/img/styles/regno-di.htm regno di due sicilie] [http://flawedamythyst.ifrance.com/img/styles/heli-.htm heli attack] [http://sm4.ifrance.com/lib/lunar-arriva/ lunar arriva su game boy advance] [http://call-shotgun.ifrance.com/view/new/princess-di/ princess di] [http://sm4.ifrance.com/lib/batman/ batman 10] [http://atrayah.ifrance.com/topic/agriturismo-pesaro.htm agriturismo pesaro] [http://dejablu503.ifrance.com/description/lib/archos-batteria.htm archos batteria] [http://horcrionebay.ifrance.com/resources/articles/citroen-c.htm citroen c5 km zero] [http://zeakk.ifrance.com/resources/articles/spining.htm spining] [http://sm4.ifrance.com/lib/fia-gt/ fia gt] [http://squoi-oop.ifrance.com/topic/monitor-acer/ monitor acer lcd 17] [http://dejablu503.ifrance.com/description/lib/la-mia.htm la mia canzone al vento] [http://alishca-st.ifrance.com/text/directory/leica-fotocamera.htm leica fotocamera] [http://horcrionebay.ifrance.com/resources/articles/dmc-fzeg.htm dmc fz20eg k] [http://lightmyfire0214.ifrance.com/topic/born-in/ born in the usa] [http://cocopuff66.ifrance.com/text/directory/assistenza-infinity/ assistenza infinity roma] [http://zeakk.ifrance.com/resources/articles/nortek-lettore.htm nortek lettore mp3 1gb] [http://sm4.ifrance.com/lib/mebye/ mebye] [http://horcrionebay.ifrance.com/resources/articles/multifunzione-scanner.htm multifunzione scanner stampante] [http://cocopuff66.ifrance.com/text/directory/gsa-b/ gsa 4163b] [http://zeakk.ifrance.com/resources/articles/parabola.htm parabola 120] [http://mfirishka.ifrance.com/view/new/dagaba.htm dagaba] [http://cocopuff66.ifrance.com/text/directory/www-inutile/ www inutile com] [http://nokros.ifrance.com/data/grand-cherokee/ grand cherokee autocarro] [http://joshuaw-wise.ifrance.com/img/styles/forche-caudine/ forche caudine] [http://horcrionebay.ifrance.com/resources/articles/asics-javelin.htm asics javelin] [http://alishca-st.ifrance.com/text/directory/pisanello.htm pisanello] [http://mfirishka.ifrance.com/view/new/profumi-armani.htm profumi armani glam city] [http://smegmacheez.ifrance.com/content/view/lg-fp/ lg f900p flatron crt 19] [http://call-shotgun.ifrance.com/view/new/malombe/ malombe] [http://lekusya.ifrance.com/library/html/video-di/ video di elisabetta canalis] [http://trumanburb.ifrance.com/view/new/sallustio.htm sallustio] [http://squoi-oop.ifrance.com/topic/mama-bbb/ mama bbb 4] [http://cocopuff66.ifrance.com/text/directory/la-signorina/ la signorina] [http://flawedamythyst.ifrance.com/img/styles/dvd-vergini.htm dvd vergini dual layer] [http://lightmyfire0214.ifrance.com/topic/nanno/ nanno] [http://joshuaw-wise.ifrance.com/img/styles/un-giorno/ un giorno dopo l altro] [http://lekusya.ifrance.com/library/html/le-roi/ le roi du] [http://joshuaw-wise.ifrance.com/img/styles/foto-di/ foto di brian krause nudo] [http://horcrionebay.ifrance.com/resources/articles/non-discriminazione.htm non discriminazione] [http://sm4.ifrance.com/lib/voglio-vedere/ voglio vedere la figa di manuela arcuri] [http://gajar.ifrance.com/html/the-sims.htm the sims triple deluxe] [http://krychan.ifrance.com/content/view/crazy-.htm crazy taxi] [http://dejablu503.ifrance.com/description/lib/francescani-dell.htm francescani dell immacolata] [http://flawedamythyst.ifrance.com/img/styles/rover-.htm rover 420 td 4p. di] [http://sm4.ifrance.com/lib/eosinofili/ eosinofili] [http://smegmacheez.ifrance.com/content/view/download-gratis/ download gratis video hard] [http://gabe95.ifrance.com/html/she-said/ she said yes] [http://lekusya.ifrance.com/library/html/calcio-football/ calcio football soccer] [http://lekusya.ifrance.com/library/html/omda/ omda] [http://alishca-st.ifrance.com/text/directory/troia-tette.htm troia tette] [http://squoi-oop.ifrance.com/topic/pumpitup-italiano/ pumpitup italiano] [http://call-shotgun.ifrance.com/view/new/epson-cartucce/ epson cartucce stylus cx5400] [http://lightmyfire0214.ifrance.com/topic/jeep-rubicon/ jeep rubicon] [http://squoi-oop.ifrance.com/topic/hp-/ hp 6 colori a3] [http://cocopuff66.ifrance.com/text/directory/amanti-di/ amanti di toledo, gli] [http://alishca-st.ifrance.com/text/directory/sofware-cable.htm sofware cable usb] [http://horcrionebay.ifrance.com/resources/articles/nad.htm nad 533] [http://nokros.ifrance.com/data/birilli-biliardo/ birilli biliardo] [http://horcrionebay.ifrance.com/resources/articles/dsn.htm dsn 1000] [http://zeakk.ifrance.com/resources/articles/fisioterapia-brescia.htm fisioterapia brescia] [http://call-shotgun.ifrance.com/view/new/coppia-travesta/ coppia travesta lecce] [http://cocopuff66.ifrance.com/text/directory/it-s/ it s all for tou] [http://lekusya.ifrance.com/library/html/bmw-/ bmw 330 d] [http://flawedamythyst.ifrance.com/img/styles/warsaw.htm warsaw] [http://gabe95.ifrance.com/html/g-apple/ g4 apple] [http://atrayah.ifrance.com/topic/sanson.htm sanson] [http://call-shotgun.ifrance.com/view/new/carne-sarda/ carne sarda] [http://nokros.ifrance.com/data/sexobizarro/ sexobizarro] [http://squoi-oop.ifrance.com/topic/tresette-a/ tresette a perdere] [http://horcrionebay.ifrance.com/resources/articles/mg-snc.htm mg snc] [http://alishca-st.ifrance.com/text/directory/velas.htm velas] [http://horcrionebay.ifrance.com/resources/articles/camping-car.htm camping car] [http://dejablu503.ifrance.com/description/lib/nissan-patrol.htm nissan patrol safari] [http://trumanburb.ifrance.com/view/new/www-erotismo.htm www erotismo it] [http://flawedamythyst.ifrance.com/img/styles/branda-per.htm branda per cane] [http://smegmacheez.ifrance.com/content/view/sexy-cherry/ sexy cherry] [http://flawedamythyst.ifrance.com/img/styles/www-temagay.htm www temagay com] [http://horcrionebay.ifrance.com/resources/articles/move-ya.htm move ya body] [http://squoi-oop.ifrance.com/topic/antenne-cellulare/ antenne cellulare] [http://smegmacheez.ifrance.com/content/view/db-budokai/ db budokai 3] [http://mfirishka.ifrance.com/view/new/radio-fm.htm radio fm digitale] [http://cocopuff66.ifrance.com/text/directory/metronomo-batteria/ metronomo batteria] [http://krychan.ifrance.com/content/view/nuova-audi.htm nuova audi a6 auto nuove] [http://zeakk.ifrance.com/resources/articles/desbordesvalmore-marceline.htm desbordes-valmore, marceline] [http://call-shotgun.ifrance.com/view/new/danii-minogue/ danii minogue] [http://call-shotgun.ifrance.com/view/new/amsterdam-alberghi/ amsterdam alberghi garni] [http://mfirishka.ifrance.com/view/new/linda-madalena.htm linda madalena] [http://gajar.ifrance.com/html/bimbio.htm bimbio] [http://krychan.ifrance.com/content/view/sylvia-saint.htm sylvia saint mpeg] [http://nokros.ifrance.com/data/nino-d/ nino d angelo senza cravatta mp3] [http://trumanburb.ifrance.com/view/new/giochi-d.htm giochi d azioni] [http://krychan.ifrance.com/content/view/weezer-beverly.htm weezer beverly hills] [http://alishca-st.ifrance.com/text/directory/pinna-picasso.htm pinna picasso] [http://dejablu503.ifrance.com/description/lib/monnalisa-smile.htm monnalisa smile] [http://horcrionebay.ifrance.com/resources/articles/condono-edilizio.htm condono edilizio regione campania] [http://sm4.ifrance.com/lib/milan-cori/ milan cori] [http://smegmacheez.ifrance.com/content/view/meeting-rimini/ meeting rimini 2004] [http://squoi-oop.ifrance.com/topic/grupo-bryndis/ grupo bryndis] [http://horcrionebay.ifrance.com/resources/articles/nokia-.htm nokia 2600 blue] [http://cocopuff66.ifrance.com/text/directory/nicomar/ nicomar] [http://sm4.ifrance.com/lib/shy-love/ shy love] [http://zeakk.ifrance.com/resources/articles/on-stage.htm on stage jbl] [http://squoi-oop.ifrance.com/topic/cavo-svideo/ cavo s-video jack 3 5] [http://sm4.ifrance.com/lib/video-pornoo/ video pornoo] [http://horcrionebay.ifrance.com/resources/articles/flash-speedlite.htm flash speedlite 220ex] [http://sm4.ifrance.com/lib/rsmmc-mb/ rs-mmc 256mb nokia] [http://horcrionebay.ifrance.com/resources/articles/copertinadvd.htm copertinadvd] [http://joshuaw-wise.ifrance.com/img/styles/porno-giappone/ porno giappone] [http://mfirishka.ifrance.com/view/new/agriturismo-cuneo.htm agriturismo cuneo] [http://squoi-oop.ifrance.com/topic/amplificatore-aiwa/ amplificatore aiwa mini] [http://gabe95.ifrance.com/html/ibn-battuta/ ibn battuta] [http://sm4.ifrance.com/lib/pantar/ pantar] [http://mfirishka.ifrance.com/view/new/nuovo-trailer.htm nuovo trailer e immagini di x-men legends ii] [http://lightmyfire0214.ifrance.com/topic/ostelli-firenze/ ostelli firenze] [http://zeakk.ifrance.com/resources/articles/sd-per.htm sd per palmari] [http://zeakk.ifrance.com/resources/articles/opel-astra.htm opel astra anno 2004] [http://mfirishka.ifrance.com/view/new/genitori-efficaci.htm genitori efficaci di gordon] [http://squoi-oop.ifrance.com/topic/download-canzoni/ download canzoni mp3] [http://sm4.ifrance.com/lib/scanner/ scanner 3200] [http://alishca-st.ifrance.com/text/directory/come-sposare.htm come sposare una figlia] [http://call-shotgun.ifrance.com/view/new/come-la/ come la cerva] [http://sm4.ifrance.com/lib/rimouski/ rimouski] [http://horcrionebay.ifrance.com/resources/articles/monte-everest.htm monte everest] [http://nokros.ifrance.com/data/rouge-et/ rouge et noir] [http://atrayah.ifrance.com/topic/haid.htm haid] [http://lekusya.ifrance.com/library/html/henry-kendall/ henry kendall] [http://dejablu503.ifrance.com/description/lib/lcd.htm lcd 14] [http://krychan.ifrance.com/content/view/perle-orecchini.htm perle orecchini rosa] [http://horcrionebay.ifrance.com/resources/articles/amore-via.htm amore via satellite] [http://lightmyfire0214.ifrance.com/topic/le-sorelle/ le sorelle platinette] [http://horcrionebay.ifrance.com/resources/articles/driver-modem.htm driver modem motorola 525] [http://lightmyfire0214.ifrance.com/topic/invertebrati-marini/ invertebrati marini] [http://joshuaw-wise.ifrance.com/img/styles/monsieur-de/ monsieur de la salle] [http://call-shotgun.ifrance.com/view/new/di-laj/ di laj laj la] [http://zeakk.ifrance.com/resources/articles/tempi-brutti.htm tempi brutti per i sergenti] [http://horcrionebay.ifrance.com/resources/articles/hoover-hca.htm hoover hca 454 pw] [http://zeakk.ifrance.com/resources/articles/dr.htm dr 7800] [http://zeakk.ifrance.com/resources/articles/home-dvd.htm home dvd recorder] [http://gabe95.ifrance.com/html/scindler-s/ scindler s list] [http://cocopuff66.ifrance.com/text/directory/futurama-foto/ futurama foto porno] [http://cocopuff66.ifrance.com/text/directory/jet-ducks/ jet ducks] [http://squoi-oop.ifrance.com/topic/iriver/ iriver 120] [http://krychan.ifrance.com/content/view/cinecity.htm cinecity] [http://krychan.ifrance.com/content/view/asus-amd.htm asus amd turion64] [http://alishca-st.ifrance.com/text/directory/wrangler-.htm wrangler 4 l] [http://flawedamythyst.ifrance.com/img/styles/eta-lulu.htm eta lulu] [http://sm4.ifrance.com/lib/case-legno/ case legno] [http://krychan.ifrance.com/content/view/to-much.htm to much love will kill you bryan may] [http://lekusya.ifrance.com/library/html/pietramontecorvino/ pietramontecorvino] [http://sm4.ifrance.com/lib/mark-il/ mark il poliziotto] [http://gabe95.ifrance.com/html/stefano-parrino/ stefano parrino] [http://gabe95.ifrance.com/html/masseria-s/ masseria s lucia] [http://horcrionebay.ifrance.com/resources/articles/sedia-regista.htm sedia regista] [http://alishca-st.ifrance.com/text/directory/charles-mingus.htm charles mingus. live at montreaux 1975] [http://zeakk.ifrance.com/resources/articles/nordica-speedmachine.htm nordica speedmachine 14] [http://squoi-oop.ifrance.com/topic/canon-ir/ canon ir 2000] [http://lightmyfire0214.ifrance.com/topic/jeep-mantova/ jeep mantova] [http://horcrionebay.ifrance.com/resources/articles/indipendet.htm indipendet] [http://atrayah.ifrance.com/topic/www-meridiano.htm www meridiano com] [http://horcrionebay.ifrance.com/resources/articles/mark-anthony.htm mark anthony] [http://joshuaw-wise.ifrance.com/img/styles/chillout/ chillout] [http://gajar.ifrance.com/html/software-gestionale.htm software gestionale personalizzato] [http://cocopuff66.ifrance.com/text/directory/standard-oil/ standard oil co. of new jersey] [http://mfirishka.ifrance.com/view/new/offerte-di.htm offerte di lavoro bolzano] [http://flawedamythyst.ifrance.com/img/styles/fagiani.htm fagiani] [http://cocopuff66.ifrance.com/text/directory/sim-park/ sim park world] [http://nokros.ifrance.com/data/gioco-di/ gioco di coppia] [http://gajar.ifrance.com/html/boll-d.htm boll 3d] [http://lightmyfire0214.ifrance.com/topic/todas-las/ todas las fotos de chibolas desnudas] [http://flawedamythyst.ifrance.com/img/styles/bandiere.htm bandiere] [http://gajar.ifrance.com/html/relly-championship.htm relly championship] [http://nokros.ifrance.com/data/foto-di/ foto di cannavaro] {{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><unsafe></code>: The class has not yet been reviewed for E safety, so <code><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 00:44, 24 August 2007
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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)