Ruby- need to fetch the xpath from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kharry
    New Member
    • Feb 2010
    • 6

    Ruby- need to fetch the xpath from a file

    Hi

    I am writing xpath for the components, but need to put and then fetch the same from a file. I need to name evey xpath and then put the reference to fetch from the file. Please let me know if this can be done.

    Thanks in advanvce
    Harry
  • improvcornartist
    Recognized Expert Contributor
    • May 2007
    • 303

    #2
    I'm not overly familiar with xpath, so I can't really tell you how to do this, but it should be doable. You might want to look into REXML.

    Comment

    • cr4sh
      Banned
      New Member
      • Jul 2010
      • 20

      #3
      please try this-
      Code:
      #!/usr/bin/ruby -w
      
      require 'rexml/document'
      include REXML
      
      xmlfile = File.new("movies.xml")
      xmldoc = Document.new(xmlfile)
      
      # Info for the first movie found
      movie = XPath.first(xmldoc, "//movie")
      p movie
      
      # Print out all the movie types
      XPath.each(xmldoc, "//type") { |e| puts e.text }
      
      # Get an array of all of the movie formats.
      names = XPath.match(xmldoc, "//format").map {|x| x.text }
      p names
      this will yield the following result

      <movie title='Enemy Behind'> ... </>
      War, Thriller
      Anime, Science Fiction
      Anime, Action
      Comedy
      ["DVD", "DVD", "DVD", "VHS"]
      Last edited by MMcCarthy; Aug 4 '10, 07:06 AM. Reason: Added Code tags [code] . . . [/code]

      Comment

      Working...