Rexml trouble parsing

No matter what I do, I can’t seem to get valid XML to load into REXML
for parsing. Anybody have any idea what I’m doing wrong?

Using Ruby 2.0.0 on a 64 bit machine with Windows 7 Home Premium. I do
use pik and I am sure the correct version is active (different gems):

$pik list
187: ruby 1.8.7 (2012-02-08 patchlevel 358) [i386-mingw32]

  • 200: ruby 2.0.0p247 (2013-06-27) [x64-mingw32]

====================
require ‘rubygems’
require ‘openssl’
require ‘net/https’
require ‘rspec’
require ‘rexml/document’
include REXML

#SSL Key
cacert_file = File.join(%w{c: Project GrabSomeData cacert.pem})
#RE: Auto-downloader here to obtain from curl.haxx.se/ca/cacert.pem

#Easy to modify URL perametres
$base_url = “https://maps.googleapis.com/
$uri = URI.parse($base_url)
places_call = “maps/api/place/nearbysearch/”
key_sensor = “key=[ENTER YOUR DEVELOPER KEY]&sensor=false”
specific_detail =
“&location=53.541195,-113.490331&radius=200&types=bakery|bar|cafe|night_club|bar|restaurant”
$xml_url = $base_url + places_call + “xml?” + key_sensor +
specific_detail

#Get the file and hold in memory
http = Net::HTTP.new($uri.host, $uri.port)
http.use_ssl = true
http.cert = OpenSSL::X509::Certificate.new(File.read cacert_file)
request = Net::HTTP::Get.new($xml_url)
response = http.request(request)

#Tried both with and without the doctype declaration, staying without
#since that was the format of the example file
#at http://www.germane-software.com/software/rexml/index.html#id2248146
xml = response.body.gsub(’<?xml version="1.0" encoding="UTF-8"?>’,’’)

#Pull in for REXML
doc = Document.new xml

Result:
… </>

The same thing happens when I save the example file and import it:

irb(main):047:0> xml = File.read(examples/Rexml.xml)
NameError: undefined local variable or method examples' for main:Object from (irb):47 from C:/Users/Julie/.pik/rubies/Ruby-200-p195/bin/irb:12:in
irb(main):048:0> xml = File.read(samples/Rexml.xml)
NameError: undefined local variable or method samples' for main:Object from (irb):48 from C:/Users/Julie/.pik/rubies/Ruby-200-p195/bin/irb:12:in
irb(main):049:0> xml = File.read(‘samples/Rexml.xml’)
=> "<inventory title=“OmniCorp Store #45x10^3”>\n <section
name=“health”>\n <item upc=“123456789” stock=“12”>\n
Invisibility Cream\n 14.50\n
Makes you invisible\n \n <item
upc=“445322344” stock=“18”>\n Levitation Salve\n
23.99\n Levitate yourself for up to 3 hours
per application\n \n \n <section
name=“food”>\n <item upc=“485672034” stock=“653”>\n <name

Blork and Freen Instameal\n 4.95\n <description
A tasty meal in a tablet; just add water\n \n
<itemupc=“132957764” stock=“44”>\n Grob winglets\n
3.56\n Tender winglets of Grob. Just add
water\n \n \n"
irb(main):050:0> doc = Document.new xml
=> … </>

Quick answer…

xml = File.read(examples/Rexml.xml) # it is missing apostrophes

xml = File.read(“examples/Rexml.xml”) # The last is correct.

doc = Document.new xml # Calm down… It is OK to show “…”

Try…

puts doc

Try

doc.root

Try:

root = doc.root # => … </>
root.attribute(“title”) # => title=‘OmniCorp Store #45x10^3’
root.attributes[“title”] # => “OmniCorp Store #45x10^3”
root.attribute(“title”).class # => REXML::Attribute
root.attributes[“title”].class # => String
root.elements[1].elements[1].elements[1].text # => “\nInvisibility
Cream”
root.elements[“section”].elements[“item”].elements[“name”].text # =>
“\nInvisibility Cream”

Abinoam Jr.

Totally awesome, all working now.

corrects are now:
http = Net::HTTP.new($uri.host, $uri.port)
http.use_ssl = true
http.cert = OpenSSL::X509::Certificate.new(File.read cacert_file)
request = Net::HTTP::Get.new($xml_url)
response = http.request(request)

#xml = fetch_ssl($xml_url,1)
xml = response.body
include REXML
doc = Document.new xml
puts doc
references = Array.new
XPath.each( doc, “//reference”) {|a| references << a.text}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs