Guest User

Untitled

a guest
Feb 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. jruby, a ruby interpreter written in java, makes it much easier to
  2. interface with java libraries from ruby than the ruby java bridge
  3. (where i couldnt, for the life of me, make java accept a pure ruby
  4. java.io.InputStream implementation). I chose woodstox's implementation
  5. of stax (a pull parser that apparently performs better than even the
  6. xerces stream parser) to play with the idea.
  7.  
  8. As an aside, doesn't the idea of controlling the flow of the parser
  9. programmatically (by calling 'next' on the reader to get an event)
  10. make so much more sense than having the entire flow of your code
  11. controlled by an xml file?
  12.  
  13.  
  14. #!/usr/bin/env jruby
  15. #
  16. # Created by Travis Tilley on 2006-12-09.
  17. # Copyright (c) 2006. All rights reserved.
  18.  
  19. require 'java'
  20.  
  21. # this class can wrap a generic ruby IO duck and make it quack like an
  22. InputStream
  23. include_class 'org.jruby.util.IOInputStream'
  24. # stax
  25. include_class 'javax.xml.stream.XMLInputFactory'
  26. include_class 'javax.xml.stream.XMLStreamException'
  27. include_class 'javax.xml.stream.XMLStreamReader'
  28.  
  29. # the java interface for accessing XMLStreamConstants doesnt like me
  30. XMLStreamConstants = {
  31. :START_ELEMENT => 1,
  32. :END_ELEMENT => 2,
  33. :PROCESSING_INSTRUCTION => 3,
  34. :CHARACTERS => 4,
  35. :COMMENT => 5,
  36. :SPACE => 6,
  37. :START_DOCUMENT => 7,
  38. :END_DOCUMENT => 8,
  39. :ENTITY_REFERENCE => 9,
  40. :ATTRIBUTE => 10,
  41. :DTD => 11,
  42. :CDATA => 12,
  43. :NAMESPACE => 13,
  44. :NOTATION_DECLARATION => 14,
  45. :ENTITY_DECLARATION => 15
  46. }
  47.  
  48. # create a standard ruby File object
  49. xml = File.new('tst.xml')
  50. # create InputStream interface to the IO object
  51. xmlstream = IOInputStream.new(xml)
  52.  
  53. # create a stax stream reader and hook it to the InputStream
  54. factory = XMLInputFactory.newInstance
  55. reader = factory.createXMLStreamReader(xmlstream)
  56.  
  57. # implement a "stream parser" on top of the pull parser (kinda)
  58. event = reader.getEventType
  59. while event
  60. case event
  61. when XMLStreamConstants[:START_DOCUMENT]
  62. puts "Start Document"
  63. when XMLStreamConstants[:START_ELEMENT]
  64. puts "Start Element: " + reader.getLocalName
  65. reader.getAttributeCount.times do |count|
  66. puts " Attribute: " + reader.getAttributeLocalName(count)
  67. puts " -> Value: " + reader.getAttributeValue(count)
  68. end
  69. when XMLStreamConstants[:CHARACTERS]
  70. puts "Text: " + reader.getText if ! reader.isWhiteSpace
  71. when XMLStreamConstants[:END_ELEMENT]
  72. puts "End Element: " + reader.getLocalName
  73. when XMLStreamConstants[:END_DOCUMENT]
  74. puts "End Document"
  75. end
  76.  
  77. if reader.hasNext
  78. event = reader.next
  79. else
  80. event = nil
  81. end
  82. end
Add Comment
Please, Sign In to add comment