XML/DOM serialization: Difference between revisions
< XML
Content added Content deleted
(PHP) |
No edit summary |
||
Line 43: | Line 43: | ||
xmlString = document.toprettyxml(" " * 4) |
xmlString = document.toprettyxml(" " * 4) |
||
==[[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 21:33, 24 January 2007
XML/DOM serialization
You are encouraged to solve this task according to the task description, using any language you may know.
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>
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)