如何将xml文件加入/合并到一个文件中并返回它?

时间:2012-04-15 15:30:11

标签: ruby-on-rails nokogiri

我正在使用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。但是,我想将xml加入get_friends_url返回的xml中。我该怎么做呢?

按照Ben Miller的回答,我的回调中出现了一个解析错误。我认为这与包含xml version的两个xml文件有关。也许组合文件不?我看到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>

1 个答案:

答案 0 :(得分:3)

一个选项是将两个XML对象转换为字符串并将它们连接起来,然后换行新的根节点。

或者你可以用Nokogiri建设者来做这件事

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