XML/Input: Difference between revisions

From Rosetta Code
< XML
Content added Content deleted
No edit summary
(Using lang tags now. J example seems incompatible. I'll get in touch with the Geshi folks.)
Line 3: Line 3:
Given the below XML fragment, extract the list of names using whatever means desired. If the only viable method is to use XPath, refer the reader to the task [[XML_and_XPath]].
Given the below XML fragment, extract the list of names using whatever means desired. If the only viable method is to use XPath, refer the reader to the task [[XML_and_XPath]].


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


Expected Output
Expected Output
Line 100: Line 100:
J's system includes several XML processing libraries. This task is probably best addressed using XPath (this is the type of problem XPath was designed to solve), but the task description implicitly discourages that method. So we can use the SAX library instead:
J's system includes several XML processing libraries. This task is probably best addressed using XPath (this is the type of problem XPath was designed to solve), but the task description implicitly discourages that method. So we can use the SAX library instead:


load'xml/sax'
<lang j> load'xml/sax'
saxclass 'Students'
saxclass 'Students'
Line 106: Line 106:
cocurrent'base'
cocurrent'base'
process_Students_ XML
process_Students_ XML</lang>
April
April
Bob
Bob
Line 114: Line 114:


and the definition of <tt>XML</tt>:
and the definition of <tt>XML</tt>:
XML =: noun define
<lang j> XML =: noun define
<Students>
<Students>
<Student Name="April" />
<Student Name="April" />
Line 122: Line 122:
<Student Name="Emily" />
<Student Name="Emily" />
</Students>
</Students>
)
)</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==

Revision as of 07:02, 12 February 2009

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

Given the below XML fragment, extract the list of names using whatever means desired. If the only viable method is to use XPath, refer the reader to the task XML_and_XPath.

<lang xml><Students>

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

</Students></lang>

Expected Output

April
Bob
Chad
Dave
Emily

ActionScript

<lang actionscript> package {

   import flash.display.Sprite;
   public class XMLReading extends Sprite
   {
       public function XMLReading()
       {
           var xml:XML = <Students>
                           <Student Name="April" />
                           <Student Name="Bob" />
                           <Student Name="Chad" />
                           <Student Name="Dave" />
                           <Student Name="Emily" />
                         </Students>;
           for each(var node:XML in xml..Student)
           {
               trace(node.@Name);
           }
       }
   }

} </lang>


C

Library: LibXML

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <string.h>
  3. include <libxml/parser.h>
  4. include <libxml/tree.h>

static void print_names(xmlNode *node) {

 xmlNode *cur_node = NULL;
 for (cur_node = node; cur_node; cur_node = cur_node->next) {
   if (cur_node->type == XML_ELEMENT_NODE) {
     if ( strcmp(cur_node->name, "Student") == 0 ) {

xmlAttr *prop = NULL; if ( (prop = xmlHasProp(cur_node, "Name")) != NULL ) { printf("%s\n", prop->children->content);

}

     }
   }
   print_names(cur_node->children);
 }

}

const char *buffer =

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

int main() {

 xmlDoc *doc = NULL;
 xmlNode *root = NULL;
 doc = xmlReadMemory(buffer, strlen(buffer), NULL, NULL, 0);
 if ( doc != NULL ) {
   root = xmlDocGetRootElement(doc);
   print_names(root);
   xmlFreeDoc(doc);
 }
 xmlCleanupParser();
 return 0;

}</lang>

J

J's system includes several XML processing libraries. This task is probably best addressed using XPath (this is the type of problem XPath was designed to solve), but the task description implicitly discourages that method. So we can use the SAX library instead:

<lang j> load'xml/sax'

   saxclass 'Students'
   startElement =: ([: smoutput 'Name' getAttribute~ [)^:('Student'-:])
   cocurrent'base'

   process_Students_ XML</lang>
April
Bob
Chad
Dave
Emily

and the definition of XML: <lang j> XML =: noun define

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

OCaml

<lang ocaml>

  1. #directory "+site-lib/xml-light" (* or maybe just "+xml-light" *) ;;
  2. #load "xml-light.cma" ;;
  1. let x = Xml.parse_string "
 <Students>
   <Student Name=\"April\" />
   <Student Name=\"Bob\" />
   <Student Name=\"Chad\" />
   <Student Name=\"Dave\" />
   <Student Name=\"Emily\" />
 </Students>"
 in
 Xml.iter (function
   (Xml.Element ("Student", [("Name", name)], [])) -> print_endline name
 |  _ -> ()) x
 ;;

April Bob Chad Dave Emily - : unit = () </lang>

Python

<lang python>import xml.dom.minidom

doc = """<Students>

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

</Students>"""

doc = xml.dom.minidom.parseString(doc)

for i in doc.getElementsByTagName("Student"):

   print i.getAttribute("Name")</lang>

Visual Basic .NET

<lang vbnet>Dim xml = <Students>

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

Dim names = (From node In xml...<Student> Select node.@Name).ToArray

For Each name In names

    Console.WriteLine(name)

Next </lang>