Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #
  2. # Loading XML Data
  3. # by Daniel Shiffman.
  4. #
  5. # This example demonstrates how to use loadXML
  6. # to retrieve data from an XML file and make objects
  7. # from that data.
  8. #
  9. # Here is what the XML looks like:
  10. #
  11. # <?xml version='1.0'?>
  12. # <bubbles>
  13. # <bubble>
  14. # <position x='160' y='103'/>
  15. # <diameter>43.19838</diameter>
  16. # <label>Happy</label>
  17. # </bubble>
  18. # <bubble>
  19. # <position x='372' y='137'/>
  20. # <diameter>52.42526</diameter>
  21. # <label>Sad</label>
  22. # </bubble>
  23. # </bubbles>
  24. #
  25. load_library 'bubble'
  26.  
  27. attr_reader :bubbles, :xml
  28.  
  29. def setup
  30. sketch_title 'Load save xml'
  31. load_data
  32. end
  33.  
  34. def draw
  35. background(255)
  36. # Display all bubbles
  37. bubbles.each do |b|
  38. b.display
  39. b.rollover(mouse_x, mouse_y)
  40. end
  41. text_align(LEFT)
  42. fill(0)
  43. text('Click to add bubbles.', 10, height - 10)
  44. end
  45.  
  46. def load_data
  47. # Load XML file
  48. @xml = loadXML(data_path('data.xml'))
  49. # Get all the child nodes named 'bubble'
  50. children = xml.get_children('bubble')
  51. sketch_title 'Load & Save Xml'
  52. # total XML elements named 'bubble'
  53. @bubbles = []
  54. children.each do |element|
  55. # The position element has two attributes: x and y
  56. position_element = element.get_child('position')
  57. # Note how with attributes we can get an integer or directly
  58. x, y = position_element.get_int('x'), position_element.get_int('y')
  59. # The diameter is the content of the child named 'diamater'
  60. diameter_element = element.get_child('diameter')
  61. # Note how with the content of an XML node, we retrieve as a
  62. # String and then convert
  63. diameter = (diameter_element.get_content).to_f
  64. # The label is the content of the child named 'label'
  65. label_element = element.get_child('label')
  66. label = label_element.get_content
  67. # Make a Bubble object out of the data read
  68. bubbles << Bubble.new(x, y, diameter, label)
  69. end
  70. end
  71.  
  72. # Still need to work on adding and deleting
  73. def mouse_pressed
  74. # Create a new XML bubble element
  75. bubble = xml.add_child('bubble')
  76. # Set the poisition element
  77. position = bubble.add_child('position')
  78. # Here we can set attributes as integers directly
  79. position.set_int('x', mouseX)
  80. position.set_int('y', mouseY)
  81. # Set the diameter element
  82. diameter = bubble.add_child('diameter')
  83. # Here for a node's content, we have to convert to a String
  84. diameter.set_content(rand(40.0..80).to_s)
  85. # Set a label
  86. label = bubble.add_child('label')
  87. label.set_content('New label')
  88. # Here we are removing the oldest bubble if there are more than 10
  89. children = xml.get_children('bubble')
  90. # If the XML file has more than 10 bubble elements
  91. xml.remove_child(children[0]) if children.length > 10
  92. # Save a new XML file
  93. saveXML(xml, data_path('data.xml'))
  94. # reload the new data
  95. load_data
  96. end
  97.  
  98. def settings
  99. size(640, 360)
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement