这种字符串如何处理?
<?xml version= "1.0 "?>
<soap:Envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/ " xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance " xmlns:xsd= "http://www.w3.org/2001/XMLSchema "> <soap:Body> <getWeatherbyCityNameResponse xmlns= "http://tempuri.org/ "> <getWeatherbyCityNameResult> <string xsi:nil= "true "/> <string> 齐齐哈尔 </string> <string> 晴 </string> <string> 6 ~ -2 ℃ </string> <string> 西北风3-4级转微风 </string> <string> 今天 </string> <string> http://www.ayandy.com/images/晴.gif </string> <string xsi:nil= "true "/> </getWeatherbyCityNameResult> </getWeatherbyCityNameResponse> </soap:Body> </soap:Envelope>
有点象XML的,不知是不是,要取出下面的内容:
齐齐哈尔
晴
6 ~ -2 ℃
西北风3-4级转微风
今天
http://www.ayandy.com/images/晴.gif
该如何做?
[解决办法]
'分析你的文字
'发现你想要的内容都在 <string> </string> 对中
'思路:1.取出第一个 <string> 及其之后的内容
' 得到 <string> 齐齐哈尔 </string> <string> 晴 </string> <string> 6 ~ -2 ℃ </string> <string> 西北风3-4级转微风 </string> <string> 今天 </string> <string> http://www.ayandy.com/images/晴.gif </string> <string xsi:nil= "true "/> </getWeatherbyCityNameResult> </getWeatherbyCityNameResponse> </soap:Body> </soap:Envelope>
' 2.取出最后一个 </string> 之前的内容,不含 最后一个 </string>
' 得到 <string> 齐齐哈尔 </string> <string> 晴 </string> <string> 6 ~ -2 ℃ </string> <string> 西北风3-4级转微风 </string> <string> 今天 </string> <string> http://www.ayandy.com/images/晴.gif
' 3.去除 所有的 <string> ,
' 得到 齐齐哈尔 </string> 晴 </string> 6 ~ -2 ℃ </string> 西北风3-4级转微风 </string> 今天 </string> http://www.ayandy.com/images/晴.gif
' 4.将字符串以 </string> 为分隔符 放入数组
' 得到结果
Dim s As String
Dim i As Integer
'给 s 赋值你的内容
'由于字符串中包含双引号 ( ") 输起来麻烦,用单引号代替
s = " <?xml version= '1.0 '?> " & _
" <soap:Envelope xmlns:soap= 'http://schemas.xmlsoap.org/soap/envelope/ ' xmlns:xsi= 'http://www.w3.org/2001/XMLSchema-instance ' xmlns:xsd= 'http://www.w3.org/2001/XMLSchema '> <soap:Body> <getWeatherbyCityNameResponse xmlns= 'http://tempuri.org/ '> <getWeatherbyCityNameResult> <string xsi:nil= 'true '/> <string> 齐齐哈尔 </string> <string> 晴 </string> <string> 6 ~ -2 ℃ </string> <string> 西北风3-4级转微风 </string> <string> 今天 </string> <string> http://www.ayandy.com/images/晴.gif </string> <string xsi:nil= 'true '/> </getWeatherbyCityNameResult> </getWeatherbyCityNameResponse> </soap:Body> </soap:Envelope> "
'替换 单引号为 双引号,
'此处不作替换也不影响结果
s = Replace(s, " ' ", Chr(34))
'取第一个 <string> 的位置
i = InStr(s, " <string> ")
'取第一个 <string> 及其 后的所有内容
s = Mid(s, i)
'取最后一个 </string> 的内容
i = InStrRev(s, " </string> ")
'取最后一个 </string> 之前的内容,不含最后一个 </string>
s = Left(s, i - 1)
'去除 <string>
s = Replace(s, " <string> ", " ")
Dim strValue() As String
'将每一项的内容放入数组中
strValue = Split(s, " </string> ")
'取每一项的内容
For i = 0 To UBound(strValue)
Debug.Print strValue(i)
Next
[解决办法]
还是过滤html标签的问题,我早上花了20Q币+100分求来的,5555555555~~~~
用正则表达式,很容易做到
工程-引用
Microsoft VBScript Regular Expressions 5.5
建一个text1.text和text2.text,Command1控件
Private Sub Command1_Click()
Dim a As String
a = Replacex(Text1.Text, " </string> ", vbNewLine)
Text2.Text = Replacex(a, " <(.*?)> ", " ")
End Sub
Private Function Replacex(sourceStr As String, oldStr As _
String, newStr As String, Optional ignoreCase As Boolean, Optional isGlobal As Boolean = True)
Dim re As New RegExp
re.Pattern = oldStr
re.Global = isGlobal
re.ignoreCase = False
Replacex = re.Replace(sourceStr, newStr)
Set re = Nothing
End Function