Solved

REXML Troubleshooting - accessing one item

Posted on 2007-11-20
7
211 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 500 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now