3

我正在使用 Last.fm API,我正在尝试获取有关某个用户的大量信息并以 xml 形式返回。所以,这是我认为的电话:

<%= form_tag fetch_user_path, :remote => true, :'data-type' => 'xml', :id => 'search' do %>
  <%= text_field_tag :q %>
<% end %>

因此,如您所见,它需要 XML,并且我正在使用 jQuery 正确处理回调。然后,在我的控制器中:

# fetch_controller.rb
def user
  username = params[:q].gsub(' ','+')
  get_info_url = "http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=#{username}&api_key=#{API_KEY}"
  get_friends_url = "http://ws.audioscrobbler.com/2.0/?method=user.getfriends&user=#{username}&api_key=#{API_KEY}"

  respond_to do |format|
    format.xml {
      begin
        @info = Nokogiri::XML(open(get_info_url))
        @friends = Nokogiri::XML(open(get_friends_url))
      rescue Exception => e
        if e.message == '400 Bad Request'
          render xml: { :error => 'User not found.' }, :status => 400
        else
          render xml: { :error => 'Connection to Last.fm failed.' }, :status => 500
        end
      else
        # Here, I want to render @info + @friends!
        render xml: @info
      end
    }
end

这样,我正确地返回了get_info_url. 但是,我想将该 xml 加入到由get_friends_url. 我该怎么做呢?

按照 Ben Miller 的回答,我的回调出现了解析错误。我认为这与两个包含xml version. 也许合并的文件没有?我看到 xml 文件正在被连接,这是它们的使用方式console.log

Error: Invalid XML: <?xml version="1.0"?>
<Combined>
  <UserInfo>
    <?xml version="1.0" encoding="utf-8"??>
    <lfm status="ok">
      <user>
        # lots of info about the user
      </user>
    </lfm>
  </UserInfo>
  <FriendInfo>
    <?xml version="1.0" encoding="utf-8"??>
      <lfm status="ok">
        <friends for="user" page="1" perpage="50" totalpages="2" total="96">
          # lots of info about the user's friends
        </friends>
      </lfm>
  </FriendInfo>
</Combined>
4

1 回答 1

4

One option it so convert the two XML object to a string and concat them, then wrap in a new root node.

Or you could do it with Nokogiri builder

def user
  username = params[:q].gsub(' ','+')
  get_info_url = "http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=#{username}&api_key=#{API_KEY}"
  get_friends_url = "http://ws.audioscrobbler.com/2.0/?method=user.getfriends&user=#{username}&api_key=#{API_KEY}"

  respond_to do |format|
    format.xml {
      begin
        info_xml = Nokogiri::XML(open(get_info_url))
        friends_xml = Nokogiri::XML(open(get_friends_url))
        builder = Nokogiri::XML::Builder.new do |xml_out|
          xml_out.Combined {
            xml_out.UserInfo {
              node = info_xml.at_xpath("//user")
              xml_out << node.to_xml.to_str
            }
            xml_out.FriendInfo {
              node = friends_xml.at_xpath("//friends")
              xml_out << node.to_xml.to_str
            }
          }
        end
      rescue Exception => e
        if e.message == '400 Bad Request'
          render xml: { :error => 'User not found.' }, :status => 400
        else
          render xml: { :error => 'Connection to Last.fm failed.' }, :status => 500
        end
      else
        render xml: builder.to_xml
      end
    }
  end
end
于 2012-04-15T15:41:50.390 回答