/ / Ruby + Nokogiri: come creare un nodo XML con attributo = valore? - rubino, xml, nokogiri

Ruby + Nokogiri: come creare un nodo XML con attributo = valore? - rubino, xml, nokogiri

Devo presentare una richiesta a un'API con XML:

http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=<CityStateLookupRequest%20USERID="xxxxxxxxxxxx"> <ZipCode ID= "0"> <Zip5>90210</Zip5> </ZipCode> </CityStateLookupRequest>

Sto cercando di usare Nokogiri per raggiungere questo obiettivo, ma non so come aggiungere USERID="xxxx.." parte. Questo è quello che ho (incompleto):

def xml_for_initial_request
builder = Nokogiri::XML::Builder.new do |xml|
xml.CityStateLookupRequest.USERIDhowdoIsetthevalue?? {
xml.Zip {
xml.Zip5 "90210"
}
}
end
end

risposte:

9 per risposta № 1

Vorrei fare come di seguito:

require "nokogiri"

builder = Nokogiri::XML::Builder.new do |xml|
xml.CityStateLookupRequest("userid" => "xxxxxx" ) {
xml.zip("id" => "10"){
xml.Zip5 "90210"
}
}
end

puts builder.to_xml
# >> <?xml version="1.0"?>
# >> <CityStateLookupRequest userid="xxxxxx">
# >>   <zip id="10">
# >>     <Zip5>90210</Zip5>
# >>   </zip>
# >> </CityStateLookupRequest>