Reputation: 608
Input: <ArrayOfSMSIncomingMessage xmlns=\"http://sms2.cdyne.com\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><SMSIncomingMessage><FromPhoneNumber>19176230250</FromPhoneNumber><IncomingMessageID>cf8ef62d-9169-4908-a527-891fca056475</IncomingMessageID><MatchedMessageID>6838594b-288f-4e9a-863c-3ad9f4d501ca</MatchedMessageID><Message>This is a test</Message><ResponseReceiveDate>2013-04-07T17:19:06.953</ResponseReceiveDate><ToPhoneNumber>13146667368</ToPhoneNumber></SMSIncomingMessage><SMSIncomingMessage><FromPhoneNumber>19176230250</FromPhoneNumber><IncomingMessageID>ebf11b38-c176-439a-a2d0-7a2bb35390df</IncomingMessageID><MatchedMessageID>6838594b-288f-4e9a-863c-3ad9f4d501ca</MatchedMessageID><Message>Does it wotk</Message><ResponseReceiveDate>2013-04-07T17:19:17.303</ResponseReceiveDate><ToPhoneNumber>13146667368</ToPhoneNumber></SMSIncomingMessage></ArrayOfSMSIncomingMessage>
Expected Output: [["191760250", "This is a test", "2013-04-07T17:19:06.953", "13146636 8"],["191760250", "Does it wotk", "2013-04-07T17:19:17.303", "131466368"]]
I am a newbie but i can't solve this problem or find an answer. The objective is to parse a text. The problem is that I put the information into an array b and then I put array b into array c. However, what happens is that c[0] becomes equal to c[1] even thought they should have different information. I don't know how to fix this.
data='"<ArrayOfSMSIncomingMessage xmlns=\"http://sms2.cdyne.com\" xmlns:i=\" <FromPhoneNumber>191760250</FromPhoneNumber>'
data=data+'<Message>This is a test</Message><ResponseReceiveDate>2013-04-07T17:19:06.953</ResponseReceiveDate>'
data=data+'<ToPhoneNumber>13146636 8</ToPhoneNumber></SMSIncomingMessage><SMSIncomingMessage><FromPhoneNumber>191760250'
data=data+'</FromPhoneNumber><Message>Does it wotk</Message><ResponseReceiveDate>2013-04-07T17:19:17.303</ResponseRecei'
data=data+'veDate><ToPhoneNumber>131466368</ToPhoneNumber></SMSIncomingMessage></ArrayOfSMSIncomingMessage>'
a=[['<FromPhoneNumber>','</FromPhoneNumber>'],['<Message>','</Message>'],
['<ResponseReceiveDate>','</ResponseReceiveDate>'],['<ToPhoneNumber>','</ToPhoneNumber>']]
b=[]
c=[]
d=true
ii=-1
while data.index(a[0][0])!=nil do
ii+=1
for i in 0..3
print "\ni is #{i} first term: #{a[i][0]} second term #{a[i][1]}\n"
b[i]= data[data.index(a[i][0])+a[i][0].length..data.index(a[i][1])-1]
print "b[i] is #{b[i]}\n"
end
print "b is #{b}\n"
print "c is #{c}\n"
c.push(b)
print "c is #{c}\n"
d=data.slice!(0,data.index('</SMSIncomingMessage>')+5)
print "d is #{d}\n"
print "data is #{data}\n"
end
Upvotes: 0
Views: 87
Reputation: 160551
You are parsing XML. Don't waste time trying to manipulate strings, because all you'll do is generate fragile code.
Instead, use a real XML parser, which lets you navigate through the structure, and pick what you want.
First, your XML is malformed, but I worked around that by supplying a closing tag, turning it into damaged XML, but not fatally so.
require 'nokogiri'
xml = '<ArrayOfSMSIncomingMessage xmlns="http://sms2.cdyne.com" xmlns:i="">
<SMSIncomingMessage>
<FromPhoneNumber>191760250</FromPhoneNumber>
<Message>This is a test</Message>
<ResponseReceiveDate>2013-04-07T17:19:06.953</ResponseReceiveDate>
<ToPhoneNumber>131466368</ToPhoneNumber>
</SMSIncomingMessage>
<SMSIncomingMessage>
<FromPhoneNumber>191760250</FromPhoneNumber>
<Message>Does it wotk</Message>
<ResponseReceiveDate>2013-04-07T17:19:17.303</ResponseReceiveDate>
<ToPhoneNumber>131466368</ToPhoneNumber>
</SMSIncomingMessage>
</ArrayOfSMSIncomingMessage>'
doc = Nokogiri::XML(xml)
pp doc.search('SMSIncomingMessage').map{ |incoming_msg|
%w[FromPhoneNumber Message ResponseReceiveDate ToPhoneNumber].map{ |n| incoming_msg.at(n).text }
}
Which outputs:
[["191760250", "This is a test", "2013-04-07T17:19:06.953", "131466368"],
["191760250", "Does it wotk", "2013-04-07T17:19:17.303", "131466368"]]
Upvotes: 1
Reputation: 34031
I really don't understand what your code is trying to accomplish, but regarding what you say isn't working as you expect, (However, what happens is that c[0] becomes equal to c[1] even thought they should have different information.), the issue is that you are pushing b
(which is a reference) onto c
, so when you change b
, you get the appearance of the contents of c
changing.
Change
c.push(b)
to
c.push(b.dup)
if you want what you push onto c
to stay the same even after you change b
.
Upvotes: 3