Ruby Xml Mapping

7,151 views
6,872 views

Published on

Yet another talk :)
This time in a lecture called “Markup Languages und Anwendungen”
Went pretty ok, but as usual I talked a little bit too fast ^_^
http://www.marc-seeger.de

Published in: Technology
1 Comment
11 Likes
Statistics
Notes
  • Here's a gem to automatically generate Ruby classes from an XML schema using the XML mapper of your choice: https://github.com/sshaw/jaxb2ruby
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
7,151
On SlideShare
0
From Embeds
0
Number of Embeds
786
Actions
Shares
0
Downloads
0
Comments
1
Likes
11
Embeds 0
No embeds

No notes for slide

Ruby Xml Mapping

  1. 1. Markup Languages und Anwendungen Marc Seeger (20488) mail@marc-seeger.de Computer Science and Media, HdM Stuttgart, WS 08/09
  2. 2. Ruby  XML Mapping Libraries  YAML 
  3. 3. A language that doesn't affect the way you think about programming, is not worth knowing Alan Perlis
  4. 4. Origin:   Yukihiro quot;Matzquot; Matsumoto  Japan 1993 1st english book: 2000  Ruby on Rails: 2004 
  5. 5. Variable Declaration:  Function Declaration:  do_something(quot;Marcquot;, 4)  MarcMarcMarc 12
  6. 6. ClassNames  method_names and variable_names  methods_asking_a_question?  slightly_dangerous_methods!  @instance_variables  $global_variables  SOME_CONSTANTS or OtherConstants 
  7. 7. Principle of Least Surprise
  8. 8. ?
  9. 9. No Primitives, Integers and floats are objects!   -1.abs => 1
  10. 10. ; No Semi-colons
  11. 11. nil, not null  nil is an object!   nil.nil? => true  nil.class => NilClass nil and false are false   everything else, including 0, is true
  12. 12. Expression oriented syntax.   Almost everything returns a value  Methods automatically return their last expression.
  13. 13. Single Inheritance   But mixins are available (= Interface with implemented methods)
  14. 14.  This module implements the mixin A class that doesn‘t do that much inheriting and mixing! We inherited that one And mixed in that one
  15. 15. Classes are always open (even built in classes)  Another Example from Rails: 1.hour.from_now
  16. 16. Procedural  Object Oriented  Functional 
  17. 17. irb(main):001:0> 42.methods => [quot;%quot;, quot;odd?quot;, quot;inspectquot;, quot;prec_iquot;, quot;<<quot;, quot;tapquot;, quot;divquot;, quot;&quot;, quot;clonequot;, quot;>>quot;, quot;p ublic_methodsquot;, quot;__send__quot;, quot;instance_variable_defined?quot;, quot;equal?quot;, quot;freezequot;, „to_symquot;, quot;*quot;, quot;ordquot;, quot;+quot;, quot;extendquot;, quot;nextquot;, quot;sendquot;, quot;roundquot;, quot;methodsquot;, quot;prec_f„, quot;-quot;, quot;even?quot;, quot;singleton_method_addedquot;, quot;divmodquot;, quot;hashquot;, quot;/quot;, quot;integer?quot;, quot;downtoquot;, quot;dupquot;, quot;instance_variablesquot;, quot;|quot;, quot;eql?quot;, quot;sizequot;, quot;object_idquot;, quot;instance_evalquot;, quot;truncatequot;, quot;~quot;, quot;idquot;, quot;to_iquot;, quot;singleton_methodsquot;, […] irb(main):005:0> nil.class => NilClass irb(main):002:0> nil.hallo_hdm NoMethodError: undefined method `hallo_hdm' for nil:NilClass from (irb):2
  18. 18. Strong typing  ▪ quot; 5 quot; / 2  „NoMethodError“ Weak typing  ▪ quot; 16 quot; / 2  8 (e.g. in Perl) Ruby is strongly typed! (Java too)
  19. 19. Explicit: int a = 5  Implicit: a = 5  Ruby is implicitly typed! (Java explicitly)
  20. 20. Static typing   The compiler checks types during compilation Dynamic typing   The compiler doesn‘t check types during compilation Ruby uses dynamic typing (Java uses static typing)
  21. 21. „Blocks are unnamed functions“
  22. 22. Define: Call: Result:
  23. 23. The current piece of What we are going the collection we are to do with it working with
  24. 24. A closure object has: •code to run (the executable) •state around the code (the scope) So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure
  25. 25. Idea: Function returning a function In action:
  26. 26. Ruby VM (Ruby 1.8)  YARV (aka Ruby 1.9)  Rubinius  MacRuby  Jruby  IronRuby  Hotruby 
  27. 27. Gem:   Search:  Installing:
  28. 28. Gem:   Usage:
  29. 29. Enough with the Jibber-Jabber
  30. 30. XML  Object   XML-Object  XmlSimple XML  Object   ROXML  XML::MAPPING  HappyMapper
  31. 31. http://xml-object.rubyforge.org/ „Tools like JSON or YAML are a much better fit for this kind of job, but one doesn’t always have that luxury.“
  32. 32. … attempts to make the accessing of small, well-formed XML structures convenient, by providing a syntax that fits well in most Ruby programs.
  33. 33. >ruby test.rb At first, Elements are checked: Bread Recepie You can get the Attributes though: an awesome recepie for bread
  34. 34. Question notation enabled?) Elements or attributes that look like booleans are “booleanized” if called by their question names (such as
  35. 35. Question notation enabled?) Elements or attributes that look like booleans are “booleanized” if called by their question names (such as
  36. 36. Question notation enabled?) Elements or attributes that look like booleans are “booleanized” if called by their question names (such as
  37. 37. Question notation enabled?) Elements or attributes that look like booleans are “booleanized” if called by their question names (such as
  38. 38. Question notation enabled?) Elements or attributes that look like booleans are “booleanized” if called by their question names (such as
  39. 39. Recursive The design of the adapters assumes parsing of the objects recursively. Deep files are bound to throw SystemStackError, but for the kinds of files I need to read, things are working fine so far. In any case, stream parsing is on the TODO list.
  40. 40. http://xml-simple.rubyforge.org/ a Ruby translation of Grant McLean's Perl module XML::Simple
  41. 41. xml_in()  xml_out() 
  42. 42. The input file A hash containing options
  43. 43. Usage:
  44. 44. http://roxml.rubyforge.org/ ROXML
  45. 45. roxml_1.0_beta roxml-1.0_beta June 28, 2006 ROXML 1.0 roxml-1.0.zip July 1, 2006 September 24, ROXML 1.1 Beta ROXML 1.1 Beta 2006 ROXML 1.2 ROXML 1.2 November 10, 2007 ROXML 2.2.0 November 3, 2008
  46. 46. ROXML Features  Read Ruby objects from XML  Write Ruby objects to XML  Annotation-style methods for XML mapping  One-to-one (composition) Ruby to XML  One-to-many (aggregation) Ruby with array to XML Source: http://roxml.rubyforge.org
  47. 47. http://xml-mapping.rubyforge.org/ ≈ROXML
  48. 48. } XML::Mapping::ArrayNode XML::Mapping::BooleanNode XML::Mapping::HashNode XML::Mapping::NumericNode single-attribute nodes XML::Mapping::ObjectNode XML::Mapping::SingleAttributeNode XML::Mapping::SubObjectBaseNode XML::Mapping::TextNode XML::Mapping::ChoiceNode
  49. 49. http://happymapper.rubyforge.org/ „Making XML fun again“
  50. 50. typecasts has many
  51. 51. Camel Case XML Tags to Ruby method names
  52. 52. (rhymes with “camel”)
  53. 53. easily readable by humans. 1. matches the native data structures of agile 2. languages. portable between programming languages. 3. consistent model to support generic tools. 4. supports one-pass processing. 5. expressive and extensible. 6. easy to implement and use. 7.
  54. 54. JSON/YAML = human readable data interchange format  JSON == simplicity + universality   trivial to generate and parse  reduced human readability YAML == human readability + serializing native data structures   harder to generate and parse  easy to read
  55. 55. JSON YAML JSON.valid? YAML.valid!
  56. 56. …is Sequences, Maps, Scalars   Seq = Array  Map = Hash  Scalars = String, Integer, Float, Time, NilClass
  57. 57. Sequence: Array:
  58. 58. Map: Hash:
  59. 59. Map of Scalars: Hash of Objects: Native typing is implicity determined in plain scalars.
  60. 60. YAML output:  YAML input: 
  61. 61. More than 1 document  Ruby code  Output 
  62. 62. Won‘t somebody please think of the children objects!
  63. 63. Live Demo :D 
  64. 64. Problem: The !ruby/object type is only understood by YAML.rb. Solution:
  65. 65. http://www.kuwata-lab.com/kwalify/  YAML and JSON are simple and nice format  for structured data and easier for human to read and write than XML. But there have been no schema for YAML such as RelaxNG or DTD. Kwalify gets over this situation.
  66. 66. Fragen?
  67. 67. Each project‘s website  Some useful closures in Ruby  http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby Kai Jäger: Ajax in der Praxis Grundlagen, Konzepte, Lösungen  ISBN-10: 3-540-69333-5 Using Ruby - An Introduction to Ruby for Java Programmers  http://onestepback.org/articles/usingruby/index.html Ruby for Java Programmers  http://www.softwaresummit.com/2006/speakers/BowlerRubyForJavaProgrammers.pdf Happy Mapper: Making XML fun again:  http://railstips.org/2008/11/17/happymapper-making-xml-fun-again YAML Working draft 1.2  http://yaml.org/spec/1.2/ YAML Cookbook:  http://www.nt.ntnu.no/users/haugwarb/Programming/YAML/YAML_for_ruby.html

×