?
Solved

REXML Troubleshooting - accessing one item

Posted on 2007-11-20
7
Medium Priority
?
232 Views
Last Modified: 2012-05-05
I'm reading the following XML file using REXML in rails.

<xml>
<event id='2288'>
<venue_id>1</venue_id>
</event>
<venue id='1'>
<name>hello</name>
</venue>
</xml>

The code I use my controller is this:

@events = droot.elements.to_a("//event")
@venues = droot.elements.to_a("//venue")

This works and my data is loaded into each array.

But the problem I'm having is accessing a specific venue from that array.

I've tried stuff like this, but no luck:

<%= @venues[@id=1].elements['name'] %>

Can anybody help me access one particular element in the array? In my view I end up looping through each event and just need to access the associated venue. (This is from the zvents API) Thanks for any help!
0
Comment
Question by:idadan
  • 4
  • 3
7 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20328243
Why are you converting the elements you found to an array?
It will no longer be accessible as XML I believe

Can't you do something like this?
XPath.each(droot, "//venue[@id = 1]/name")
It will give you that name directly

cheers

Geert

0
 

Author Comment

by:idadan
ID: 20332209
I guess the reason I was converting them to an array was just because it was easy to then access them in the view.

The problem is though I will end up with more id's than just 1.  All the examples I see online are people knowing the ID they're going to reference, I'll have many.

I just need some way in my view to loop through events, and be able to access the associated venue info.  Thanks for any help!

(So my real xml stream would look more like this)

<xml>
<event id='2288'>
<venue_id>1</venue_id>
</event>
<event id='3773'>
<venue_id>2</venue_id>
</event>
...etc...
<venue id='1'>
<name>hello</name>
</venue>
<venue id='2'>
<name>numero 2</name>
</venue>
...etc...
</xml>
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 20333048
well, you could make a hash from it

require 'open-uri'
require 'rexml/document'
include REXML
 
open("test.xml") {|f|   @req = f.read }

doc = Document.new(@req)
name_list = Hash.new
 
XPath.each(doc, "//venue") do |element|
            XPath.each(element, "@id") do |id|
                  XPath.each(element, "name/text()") do |name|
                        name_list[id] = name
                  end
            end
end

name_list.each {|key, val| puts key, val}

as you see from this example,
you don't need to put it in a hash
XPath creates a nodelist, you don't necessarily need to map that to an array,
you can wok the nodelist

cheers

Geert
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:idadan
ID: 20334655
This is the code as I have it now.  I still can't actually reference the name in the view, I know you gave the each key,val example, but what I'm I doing wrong?!  Thanks for the help, I think I'm almost there...!

CONTROLLER:

        @events = droot.elements.to_a("//event")
        @name_list = Hash.new
 
        XPath.each(droot, "//venue") do |element|
            XPath.each(element, "@id") do |id|
                  XPath.each(element, "name/text()") do |name|
                        @name_list[id] = name
                  end
            end
          end

VIEW
@name_list[7358].name  <--- This is nil though when I try to access it...

Thank you so much for the help, I really appreciate it.  
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 2000 total points
ID: 20334663
maybe you need to explicitely cast to strings
                      @name_list[id.to_s] = name.to_s
they might still be nodes instead of strings

and then you need string values anyway
@name_list["7358"].name

now, that should work

cheers

Geert
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20335147
> now, that should work

it does with my test file
0
 

Author Closing Comment

by:idadan
ID: 31410167
In the end in my view I changed it to this to get it to display:
<%= @name_list[event.elements['venue_id'].text] %>

Thanks so much Gertone, you rock!  I really appreciate the help!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question