Anonymous user
XML/DOM serialization: Difference between revisions
Undo revision 7941 by Special:Contributions/Cf2Nem (User talk:Cf2Nem)
mNo edit summary |
(Undo revision 7941 by Special:Contributions/Cf2Nem (User talk:Cf2Nem)) |
||
Line 1:
{{task}}
Create a simple DOM and having it serialize to:
Line 29:
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.)
==[[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)
|