0
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"]]

我是新手,但我无法解决这个问题或找到答案。目标是解析文本。问题是我将信息放入数组 b 中,然后将数组 b 放入数组 c 中。然而,发生的事情是 c[0] 变得等于 c[1],即使他们认为它们应该有不同的信息。我不知道如何解决这个问题。

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
4

2 回答 2

3

我真的不明白你的代码试图完成什么,但是关于你所说的并没有像你期望的那样工作,(但是,发生的事情是 c[0] 变得等于 c[1] 甚至认为他们应该有不同的信息。),问题是你正在推送b(这是一个参考)c,所以当你改变时b,你会得到改变内容的外观c

改变

c.push(b)

c.push(b.dup)

如果你希望你所推动的东西c即使在你改变之后也保持不变b

于 2013-04-08T19:18:44.990 回答
1

您正在解析 XML。不要浪费时间尝试操作字符串,因为您所要做的就是生成脆弱的代码。

相反,使用真正的 XML 解析器,它可以让您在结构中导航,并选择您想要的。

首先,您的 XML 格式不正确,但我通过提供一个结束标记来解决这个问题,将其变成损坏的 XML,但不是致命的。

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 }
}

哪个输出:

[["191760250", "This is a test", "2013-04-07T17:19:06.953", "131466368"],
["191760250", "Does it wotk", "2013-04-07T17:19:17.303", "131466368"]]
于 2013-04-08T20:37:19.433 回答