What I’m trying to do is read the value for all the nodes in this XML and put them into an array. This should be simple but for some reason it’s driving me nuts.
XML
<ArrayOfAddress>
<Address>
<AddressId>297424fe-cfff-4ee1-8faa-162971d2645f</AddressId>
<FirstName>George</FirstName>
<LastName>Washington</LastName>
<Address1>123 Main St</Address1>
<Address2>Apt #611</Address2>
<City>New York</City>
<State>NY</State>
<PostalCode>10110</PostalCode>
<CountryCode>US</CountryCode>
<EmailAddress>test@test.com</EmailAddress>
<PhoneNumber>5555551234</PhoneNumber>
<AddressType>CustomerAddress</AddressType>
</Address>
</ArrayOfAddress>
Code
class MassageRepsone
def parse_resp
@@get_address.url_builder #URL passed through HTTPClient - @@resp is the xml above
doc = Nokogiri::XML::Reader(@@resp)
@@values = doc.each do |node|
node.value
end
end
@@get_address.parse_resp
obj = [@@values]
Array(obj)
p obj
end
The code snippet from above returns the following:
297424fe-cfff-4ee1-8faa-162971d2645f
George
Washington
123 Main St
Apt #622
New York
NY
10110
US
test.test.com
5555551234
CustomerAddress
I tried putting @@values to a string and applying chomp but that just prints the newlines as nil and puts quotes around the values. Not sure what the next step is or if I need to approach this differently with Nokogiri.
Your problem is that this code…
…calls
node.valueon each node, but then doesn’t do anything with the result.Array#eachreturns the array that was iterated, and that’s what you are setting@@valuesto. Butdoc.eachdoesn’t have all the nodes in the document.Perhaps you want:
It’s hard to be sure because you don’t explain what the array ought to look like in the end. Perhaps you want:
…which would give you an array of one array for each
<Address>element, filled with the values in that element.