XML/Output: Difference between revisions

From Rosetta Code
< XML
Content added Content deleted
m (→‎{{header|Tcl}}: add example for previous edit)
m (→‎{{header|Tcl}}: link to tdom library)
Line 77: Line 77:
</pre>
</pre>


Using [http://www.tdom.org tDOM]
Using {{libheader|tDOM}}
<lang tcl>package require tdom
<lang tcl>package require tdom
set xml [dom createDocument Students]
set xml [dom createDocument Students]
Line 93: Line 93:
<Student Name="Emily"/>
<Student Name="Emily"/>
</Students></pre>
</Students></pre>
Using [http://tclxml.sf.net TclDOM]
Using {{libheader|TclDOM}}
<lang tcl>package require dom
<lang tcl>package require dom
set xml [dom::DOMImplementation create]
set xml [dom::DOMImplementation create]

Revision as of 16:28, 15 May 2009

Task
XML/Output
You are encouraged to solve this task according to the task description, using any language you may know.

Given the list of names and the below template, generate an XML document.

<Students>
  <Student Name="XXX" />
</Students>

Sample Output

<Students>
  <Student Name="April" />
  <Student Name="Bob" />
  <Student Name="Chad" />
  <Student Name="Dave" />
  <Student Name="Emily" />
</Students>

OCaml

<lang ocaml>

  1. #directory "+site-lib/xml-light" (* or maybe just "+xml-light" *) ;;
  1. #load "xml-light.cma" ;;
  1. let students = ["April"; "Bob"; "Chad"; "Dave"; "Emily"] in
 let tags_students = List.map (fun name ->
     Xml.Element ("Student", [("Name", name)], [])
   ) students in
 print_endline (
   Xml.to_string_fmt (Xml.Element ("Students", [], tags_students)))
 ;;

<Students>

 <Student Name="April"/>
 <Student Name="Bob"/>
 <Student Name="Chad"/>
 <Student Name="Dave"/>
 <Student Name="Emily"/>

</Students> - : unit = () </lang>

Visual Basic .NET

<lang vbnet>Dim names As String() = New String() {"April", "Bob", "Chad", "Dave", "Emily"}

Dim xml = <Students>

             <%= From s In names Select <Student Name=<%= s %>/> %>
         </Students>

Console.WriteLine(xml)</lang>

Tcl

Using only Tcl string manipulation: <lang Tcl> proc < {name attl args} {

   set res <$name
   foreach {att val} $attl {
       append res " $att='[string map {' '} $val]'"
   }
   if {[llength $args]} {
       append res >
       foreach a $args {
           append res \n$a
       } 
       append res </$name>
   } else {append res />}
   return $res

} set cmd {< Students {}} foreach XXX {April Bob Chad Dave Emily O'Connor} {

   append cmd " \[< Student {Name $XXX}\]"

} puts [eval $cmd]</lang>

produces

<Students>
<Student Name='April'/>
<Student Name='Bob'/>
<Student Name='Chad'/>
<Student Name='Dave'/>
<Student Name='Emily'/>
<Student Name='O'Connor'/></Students>

Using

Library: tDOM

<lang tcl>package require tdom set xml [dom createDocument Students] foreach name {April Bob Chad Dave Emily} {

   set element [$xml createElement Student]
   $element setAttribute Name $name
   [$xml documentElement] appendChild $element

} $xml asXML</lang>

<Students>
    <Student Name="April"/>
    <Student Name="Bob"/>
    <Student Name="Chad"/>
    <Student Name="Dave"/>
    <Student Name="Emily"/>
</Students>

Using

Library: TclDOM

<lang tcl>package require dom set xml [dom::DOMImplementation create] set root [dom::document createElement $xml Students] foreach name {April Bob Chad Dave Emily} {

   set element [dom::document createElement $root Student]
   dom::element setAttribute $element Name $name

} dom::DOMImplementation serialize $xml</lang>

<?xml version='1.0'?>
<!DOCTYPE Students>
<Students><Student Name="April"/><Student Name="Bob"/><Student Name="Chad"/><Student Name="Dave"/><Student Name="Emily"/></Students>