我想使用 Nokogiri 解析 XML 中的值。
<?xml version="1.0" encoding="UTF-8"?>
<payment_response>
<transaction_type>intersolve</transaction_type>
<status>declined</status>
<unique_id>a092ae8bac0ebc8c63dae3e0bd46331c</unique_id>
<transaction_id>fa3fw6dwdeadesr7rd8e1c080600d6f5</transaction_id>
<code>540</code>
<technical_message>Amount exceeds credit card limit.</technical_message>
<message>Amount exceeds credit card limit.</message>
<mode>test</mode>
<timestamp>2017-11-23T12:36:25Z</timestamp>
<descriptor>test</descriptor>
<amount>1000</amount>
<currency>EUR</currency>
<sent_to_acquirer>false</sent_to_acquirer>
</payment_response>
payment_response = Nori.new(:parser => :nokogiri, :advanced_typecasting => false).parse(@response)
如何从payment_response
获取值?
答案 0 :(得分:2)
您可以使用xpath执行此操作:
`start = 'yes'
temp = 'yes'
print('Start')
if start == 'yes':
if temp == 'yes':
temp_input = input('Is it 6 am?')
temp = 'no'
if temp_input == 'yes':
print('Checking temperature == 22 degrees')
temp_input = 'no'
if temp_input == 'no':
input2 = input('Is it 9 am?')`
使用Nori:
require 'nokogiri'
XML = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<payment_response>
<transaction_type>intersolve</transaction_type>
<status>declined</status>
<unique_id>a092ae8bac0ebc8c63dae3e0bd46331c</unique_id>
<transaction_id>fa3fw6dwdeadesr7rd8e1c080600d6f5</transaction_id>
<code>540</code>
<technical_message>Amount exceeds credit card limit.</technical_message>
<message>Amount exceeds credit card limit.</message>
<mode>test</mode>
<timestamp>2017-11-23T12:36:25Z</timestamp>
<descriptor>test</descriptor>
<amount>1000</amount>
<currency>EUR</currency>
<sent_to_acquirer>false</sent_to_acquirer>
</payment_response>
XML
doc = Nokogiri::XML XML
doc.xpath('//payment_response/*').each { |element| puts element }
# <transaction_id>fa3fw6dwdeadesr7rd8e1c080600d6f5</transaction_id>
# <code>540</code>
# <technical_message>Amount exceeds credit card limit.
答案 1 :(得分:0)
使用&#39; nori&#39;:
require 'nokogiri'
require 'nori'
@response = '<?xml version="1.0" encoding="UTF-8"?>
<payment_response>
<transaction_type>intersolve</transaction_type>
<status>declined</status>
<unique_id>a092ae8bac0ebc8c63dae3e0bd46331c</unique_id>
<transaction_id>fa3fw6dwdeadesr7rd8e1c080600d6f5</transaction_id>
<code>540</code>
<technical_message>Amount exceeds credit card limit.</technical_message>
<message>Amount exceeds credit card limit.</message>
<mode>test</mode>
<timestamp>2017-11-23T12:36:25Z</timestamp>
<descriptor>test</descriptor>
<amount>1000</amount>
<currency>EUR</currency>
<sent_to_acquirer>false</sent_to_acquirer>
</payment_response>'
full_response = Nori.new(:parser => :nokogiri, :advanced_typecasting
=> false).parse(@response)
在这里,您可以使用密钥payment_response
获取哈希值。你正在寻找的是它的价值
full_response.map {|k1,v1|
# puts "%s : %s /n" % [k1,v1]
if v1.instance_of? Hash
v1.map {|k2, v2|
puts "%s : %s" % [k2,v2] # or [k2,v1[k2]]
}
end
}
我的输出是:
transaction_type : intersolve
status : declined
unique_id : a092ae8bac0ebc8c63dae3e0bd46331c
transaction_id : fa3fw6dwdeadesr7rd8e1c080600d6f5
code : 540
technical_message : Amount exceeds credit card limit.
message : Amount exceeds credit card limit.
mode : test
timestamp : 2017-11-23T12:36:25Z
descriptor : test
amount : 1000
currency : EUR
sent_to_acquirer : false