Tag: xml

什么是在ruby中格式化xml字符串的最佳方法?

给出一个像这样的xml字符串: value 什么是最好的选择(使用ruby)将其格式化为可读的东西: value

有人可以提供一个如何使用HTTParty和Ruby on Rails发布XML的示例吗?

我需要将一些xml发布到web服务,我正在尝试使用HTTParty。 有人可以提供一个关于我如何这样做的例子吗? 以下是我需要发布的XML格式: 到目前为止,这是我的class级: require ‘httparty’ class Webservice include HTTParty format :xml base_uri ‘mysite.com’ default_params :authorization => ‘xxxxxxx’ def self.add_candidate(first_name,last_name,email,gender) post(‘/test.xml’, :body => “”) end end 我不太确定如何充实add_candidate。 任何帮助,将不胜感激。 谢谢。

想要在to_xml和to_json中保持一致的行为

所以,我正在编写一项服务,在轨道上,我对人们的联系方式非常漠不关心。 我在http层进行身份validation,所以如果人们想要xml或者json,我会更少关心。 我正在重写to_json和to_xml,但我对两者使用相同的参数。 ‘包括这个’,’不包括’,等等。 其他人如何做到这一点来检查行为是否相同? 也许只需设置一组参数并在两种方法中使用它们? 或者可能有方法来validationJSON.from_xml(myobject.to_xml)== myobject.to_json? 如果我能帮忙的话,我不想两次做我的所有测试。

将XML文件中的数据导入MySQL数据库

我想构建一个Rails应用程序,将数据从XML文件导入MySQL数据库。 有帮助吗?

XML => HTML与Hpricot和Rails

我从未使用过Web服务和rails,显然这是我需要学习的东西。 我选择使用hpricot,因为它看起来很棒。 无论如何,_why足以在hpricot网站上提供以下示例: #!ruby require ‘hpricot’ require ‘open-uri’ # load the RedHanded home page doc = Hpricot(open(“http://redhanded.hobix.com/index.html”)) # change the CSS class on links (doc/”span.entryPermalink”).set(“class”, “newLinks”) # remove the sidebar (doc/”#sidebar”).remove # print the altered HTML puts doc 这看起来简单,优雅,易于peasey。 在Ruby中运行得很好,但我的问题是:如何在rails中解决这个问题? 我尝试将所有内容添加到单个控制器中,但无法想到在视图中调用它的最佳方法。 因此,如果您正在从Web API解析XML文件并使用Hpricot以干净的HTML打印它,那么您将如何分解模型,视图和控制器上的活动,以及您将放在哪里?

在使用from_xml转换为哈希时,如何使用XML属性?

我有一个XML元素,其中包含多个使用相同ID的子元素。 子元素可通过“ size ”属性区分。 当我使用from_xml创建一个哈希时,它会将所有这些子元素1字段from_xml 。 如何根据XML属性将这些子元素解析到不同的字段? XML: KISS http://userserve-ak.last.fm/serve/34/3329738.jpg http://userserve-ak.last.fm/serve/64/3329738.jpg http://userserve-ak.last.fm/serve/126/3329738.jpg http://userserve-ak.last.fm/serve/252/3329738.jpg http://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg ruby: >>xml = “KISShttp://userserve-ak.last.fm/serve/34/3329738.jpghttp://userserve-ak.last.fm/serve/64/3329738.jpghttp://userserve-ak.last.fm/serve/126/3329738.jpghttp://userserve-ak.last.fm/serve/252/3329738.jpghttp://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg” => “KISShttp://userserve-ak.last.fm/serve/34/3329738.jpghttp://userserve-ak.last.fm/serve/64/3329738.jpghttp://userserve-ak.last.fm/serve/126/3329738.jpghttp://userserve-ak.last.fm/serve/252/3329738.jpghttp://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg” >>h = Hash.from_xml xml => {“artist”=>{“name”=>”KISS”, “rank”=>”48”, “image”=>[“http://sofzh.miximages.com/ruby-on-rails/3329738.jpg”, “http://sofzh.miximages.com/ruby-on-rails/3329738.jpg”, “http://sofzh.miximages.com/ruby-on-rails/3329738.jpg”, “http://sofzh.miximages.com/ruby-on-rails/3329738.jpg”, “http://sofzh.miximages.com/ruby-on-rails/KISS+04.jpg”]}} 我想要的是: => {“artist”=>{“name”=>”KISS”, “rank”=>”48”, “image_small”=>”http://sofzh.miximages.com/ruby-on-rails/3329738.jpg”, “image_medium”=”http://sofzh.miximages.com/ruby-on-rails/3329738.jpg”, “image_large”=”http://sofzh.miximages.com/ruby-on-rails/3329738.jpg”, “image_extralarge”=”http://sofzh.miximages.com/ruby-on-rails/3329738.jpg”, image_mega”=”http://sofzh.miximages.com/ruby-on-rails/KISS+04.jpg”}}

Rails XML构建器无法呈现

我在Rails 4应用程序中的请求的响应主体中呈现XML时遇到问题。 在下面的示例中,响应正文为空白。 我在模板中放了一个调试器,所以我知道它贯穿它但不会渲染任何东西。 我创建了一个简单的rails应用程序来演示我使用构建器返回xml的问题。 任何人都能指出这个例子的(可能是愚蠢的简单)问题吗? 这是控制器,模板和测试: 控制器/ bars_controller.rb require ‘builder’ class BarsController < ApplicationController before_action :set_bar, only: [:show] # GET /bars/1 # GET /bars/1.json def show @xml = Builder::XmlMarkup.new render template: 'bars/show.xml.builder', formats: [:xml] end private # Use callbacks to share common setup or constraints between actions. def set_bar @bar = Bar.find(params[:id]) end # […]

使用cache_digests与XML构建器?

有没有办法在Rails 4(cache_digests)中利用XML的新缓存策略? 我想我可以使用xml.erb视图,但我更喜欢xml.builder视图的简洁性。 有没有办法以这种方式使用cache_digests?

使用Savon在Ruby on Rails中调用SOAP在包络和主操作方面变得奇怪

在使用Savon.rb的rails项目中,我试图进行一个非常复杂的SOAP调用。 至少复杂到Savon构建器的扩展很麻烦所以我决定直接操作:xml。 首先我发起客户: @client = Savon.client( :endpoint => ‘https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/BarcodeWebService.svc’, :wsdl => ‘https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/?wsdl’) 然后我打电话的形状为: @request = @client.build_request(:generate_barcode, xml: %Q{ … see soap call (with ruby interpolation) … } 我添加了一个格式正确的l Time.now, format: :postnl_api字符串,其余的仍然是硬编码的。 包括消息号。 以下是Savon在这种情况下实际调用的方式,如使用@request.body检索的。 我的应用程序中的SOAP调用 http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode devc_!R4xc8p9 xxxxxxxx DEVC 11223344 3S DEVC 1000000-2000000 接下来是一个调用应该如何看待,因为我看到这个调用在公司的沙箱环境中取得了成功。 应该是SOAP调用 http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode devc_!R4xc8p9 xxxxxxxx 5 28-06-2017 14:15:41 DEVC 11223344 3S DEVC 1000000-2000000 […]

使用ActiveResource将CamelCase xml / json转换为ruby命名属性

我正在使用ActiveResource来使用REST服务。 服务中的xml如下所示: Kevin Berridge ActiveResource解析这个很好,但它逐字地使用名称。 所以模型类看起来像: p = Person.find(1) p.FirstName p.LastName 我更喜欢这是否符合Ruby命名约定,如下所示: p = Person.find(1) p.first_name p.last_name ActiveResource有办法做到这一点吗? 有没有办法可以挂钩到ActiveResource并添加它?