welcome to homepage of Michal Molhanec

Absolutely incompatible with IE. Sorry. Try Mozilla Firefox or Opera instead.

Simple comparsion of programming in different languages.

Content

Introduction

When I was reading tutorial article about Cocoa (warning: the article is in Czech), I've got an idea. The article presented extremely simple RSS reader written in few lines of Objective C. I think it's good example of simple but useful application. I've ported the code to other languages for comparsion and here are the results :-). Any contributions are welcomed!

PHP 5 — clear winner

 1  <?php
 2       echo "Hello, here are world news:\n\n\n";
 3       $xml = simplexml_load_file('http://news.bbc.co.uk/rss/newsonline_uk_edition/world/rss091.xml');
 4       foreach ($xml->xpath('//description') as $desc) {
 5            echo "$desc\n\n";
 6       }
 7  ?>
Back to content.

XSLT — XSLT solution is by one line shorter then PHP

Sent by Jirka Kosek

 1  <html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
 2       Hello, here are world news:<br/>
 3       <xsl:for-each select="document('http://news.bbc.co.uk/rss/newsonline_uk_edition/world/rss091.xml')//description">
 4            <xsl:value-of select="."/><br/>
 5       </xsl:for-each>
 6  </html>

OK, not so short if you want plain text output:

 1  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 2       <xsl:output method="text"/>
 3       <xsl:template match="/">
 4            <xsl:text>Hello, here are world news:&#xA;&#xA;&#xA;</xsl:text>
 5            <xsl:for-each select="document('http://news.bbc.co.uk/rss/newsonline_uk_edition/world/rss091.xml')//description">
 6                 <xsl:value-of select="."/>
 7                 <xsl:text>&#xA;</xsl:text>
 8            </xsl:for-each>
 9       </xsl:template>
10  </xsl:stylesheet>
Back to content.

Python DOM

Sent by Ivo Danihelka

 1  from urllib2 import urlopen
 2  from xml.dom.minidom import parse
 3
 4  f = urlopen("http://news.bbc.co.uk/rss/newsonline_uk_edition/world/rss091.xml")
 5  print "Hello, here are world news:\n\n"
 6  for desc in parse(f).getElementsByTagName("description"):
 7      text = ""
 8      for child in desc.childNodes:
 9          if child.TEXT_NODE == child.nodeType:
10              text += child.data
11      print text + "\n"
Back to content.

Python SAX

 1  from urllib2 import *
 2  from xml.sax import *
 3  from xml.sax.handler import *
 4
 5  class RssContentHandler(ContentHandler):
 6       def __init__(self):
 7            self.in_desc = False
 8       def startElement(self, elem, attrs):
 9            if elem == 'description':
10                 self.in_desc = True
11       def endElement(self, elem):
12            if self.in_desc:
13                 print '\n'
14                 self.in_desc = False
15       def characters(self, content):
16            if self.in_desc:
17                 print content.encode('ascii', 'ignore'),
18
19  print "Hello, here are world news:\n\n"
20  parse(urlopen("http://news.bbc.co.uk/rss/newsonline_uk_edition/world/rss091.xml"), RssContentHandler())
Back to content.

C++ with Xerces-C++ 2.5.0

 1  #include <cstdlib>
 2  #include <iostream>
 3  #include <string>
 4  using namespace std;
 5
 6  #include <xercesc/sax2/SAX2XMLReader.hpp>
 7  #include <xercesc/sax2/XMLReaderFactory.hpp>
 8  #include <xercesc/sax2/DefaultHandler.hpp>
 9  using namespace XERCES_CPP_NAMESPACE;
10
11  class RssContentHandler : public DefaultHandler {
12       bool in_desc;
13  public:
14       RssContentHandler() : in_desc(false) {}
15       void startElement(const XMLCh *const uri, const XMLCh *const name,
16                         const XMLCh *const qname, const Attributes &attrs)
17       {
18            if (name == wstring(L"description")) {
19                 in_desc = true;
20            }
21       }
22       void endElement(const XMLCh *const uri, const XMLCh *const name, const XMLCh *const qname)
23       {
24            if (in_desc) {
25                 wcout << "\n\n";
26                 in_desc = false;
27            }
28       }
29       void characters(const XMLCh *const chars, const unsigned int length)
30       {
31            if (in_desc) {
32                 for (unsigned i = 0; i < length; i++) {
33                      wcout << chars[i];
34                 }
35            }
36       }
37  };
38
39  int main() {
40       XMLPlatformUtils::Initialize();
41       wcout << L"Hello, here are world news:\n\n";
42       SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
43       RssContentHandler content_handler;
44       parser->setContentHandler(&content_handler);
45       parser->parse("http://news.bbc.co.uk/rss/newsonline_uk_edition/world/rss091.xml");
46       delete parser;
47       return EXIT_SUCCESS;
48  }
Back to content.

Java

 1  import java.net.*;
 2  import org.xml.sax.helpers.*;
 3  import org.xml.sax.*;
 4
 5  class simple_rss extends DefaultHandler {
 6
 7  private boolean inDesc = false;
 8
 9  public void startElement (String uri, String name, String qName, Attributes atts) {
10       if (qName.equals("description")) {
11            inDesc = true;
12       }
13  }
14
15  public void endElement (String uri, String name, String qName) {
16       if (inDesc) {
17            System.out.println("\n");
18            inDesc = false;
19       }
20  }
21
22  public void characters (char ch[], int start, int length) {
23       if (inDesc) {
24            for (int i=0; i<length; i++) {
25                 System.out.print(ch[start+i]);
26            }
27       }
28  }
29
30  static public void main(String[] args) throws Exception {
31       System.out.println("Hello, here are world news:\n\n");
32       XMLReader xr = XMLReaderFactory.createXMLReader();
33       simple_rss instance = new simple_rss();
34       xr.setContentHandler(instance);
35       URL url = new URL("http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/world/rss091.xml");
36       xr.parse(new InputSource(url.openStream()));
37  }
38
39  }
Back to content.

C#

 1  using System;
 2  using System.Xml;
 3
 4  public class SimpleRss {
 5       public static void Main() {
 6            Console.WriteLine("Hello, here are world news:\n\n");
 7            XmlTextReader reader = new XmlTextReader("http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/world/rss091.xml");
 8            bool inDesc = false;
 9            while (reader.Read()) {
10                 switch (reader.NodeType) {
11                      case XmlNodeType.Element:
12                           if (reader.Name == "description") {
13                                inDesc = true;
14                           }
15                           break;
16                      case XmlNodeType.EndElement:
17                           if (inDesc) {
18                                Console.WriteLine("\n");
19                                inDesc = false;
20                           }
21                           break;
22                      case XmlNodeType.Text:
23                           if (inDesc) {
24                                Console.Write(reader.Value);
25                           }
26                           break;
27                 }
28            }
29       }
30  }
Back to content.

VB .NET

 1  Imports System
 2  Imports System.Xml
 3
 4  Public Class SimpleRss
 5
 6       Shared Sub Main
 7            Console.WriteLine("Hello, here are world news:")
 8            Console.WriteLine()
 9            Dim reader As XmlTextReader
10            reader = new XmlTextReader("http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/world/rss091.xml")
11            Dim inDesc As Boolean = False
12
13            While reader.Read()
14                 Select (reader.NodeType)
15                      Case XmlNodeType.Element:
16                           If reader.Name = "description"
17                                inDesc = True
18                           End If
19                      Case XmlNodeType.EndElement:
20                           If inDesc
21                                Console.WriteLine()
22                                Console.WriteLine()
23                                inDesc = False
24                           End If
25                      Case XmlNodeType.Text:
26                           if inDesc
27                                Console.Write(reader.Value)
28                           End if
29                 End Select
30            End While
31       End Sub
32
33  End Class
Back to content.

Comments

Comment by Michal Molhanec on 2005-07-14 15:33:55 .
Of course

The goal is not to count lines, just to have some feeling from the language.                

Comment by tdc(at)phreaker(dot)net on 2005-07-14 14:35:21 .
srovnani

tak se tu koukam srovnava nesrovnatelne :) napriklad u PHP5 se pouziva in-line zapis a u pythonu (DOM ukazka) to jde pres promennou. tudiz pocitani na pocet "radku" je dosti nesmyslny, protoze pri trose snahy by ten pythonovsky kod sel mozna napsat i jako (uznavam, ze osklivy) oneline script. kdyz bych se chtel dostat na podobny pocet radku, slo by to zmenit asi nejak takhle:


1 from urllib2 import urlopen
2 from xml.dom.minidom import parse
3 print "Hello, here are world news:\n\n"
4 for desc in parse(urlopen("http://news.bbc.co.uk/rss/newsonline_uk_edition/world/rss091.xml")).getElementsByTagName("description"):
5 print [child.data for child in desc.childNodes if child.TEXT_NODE == child.nodeType][0],"\n"

Add new comment


Spam protection: Please type the number 2 into the textbox:


No ePatents!