我从Web服务获得以下XML数据:
<ROWS>
<ROW>
<GroupBy>1</GroupBy>
<QUERY>
INSERT INTO TASKS_GVT([PK],[TaskYear],[TaskTypeId],[TaskElementType],[TaskPriority],[ElementId],[taskstatus],[TaskSubStatus],[AllocatedteamId],[PreRequisiteTask], [xloc],[yloc],[X],[Y],[Address],[TaskZone],[taskread],[clientid],[distance],[lastdistcalcdt],[ReplicateMe],[AllocationRequestTime]) VALUES ('3179543',2014,'19','1','2','107-08-106_421','3','6','380','0','0.0000000','0.0000000','44.4144000','24.5186000','الدوادمي ','421-0-0','R','0','0',GETDATE(),0,GETDATE())
</QUERY>
</ROW>
<ROW>
<GroupBy>2</GroupBy>
<QUERY>
INSERT INTO RPTProblemTypes_GVT ([TaskYear] ,[TaskCode] ,[pbTypeId] ,[ReportDate] ,[INSTANCENAME] ,[PK] ,[ParentId] ) VALUES ('2015','3179543','754','Sep 11 2015 11:11AM','1-1086','380-1','1086')
</QUERY>
</ROW>
<ROW>
</ROWS>
我正在使用NSXMParser
这样阅读Web服务响应:
let parser = NSXMLParser (data: data!) //data is fetched using alamofire
委托方法:
public func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
{
tagElement=elementName
}
public func parser(parser: NSXMLParser, foundCharacters string: String)
{
if (tagElement.uppercaseString == "QUERY")
{
dataReturned+=string;
print ("here dataReturned: \(string)")
}
}
public func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
}
我的输出是:
here dataReturned: INSERT INTO TASKS_GVT([PK],[TaskYear],[TaskTypeId],[TaskElementType],[TaskPriority],[ElementId],[taskstatus],[TaskSubStatus],[AllocatedteamId],[PreRequisiteTask],
here dataReturned:
[xloc],[yloc],[X],[Y],[Address],[TaskZone],[taskread],[clientid],[distance],[lastdistcalcdt],[ReplicateMe],[AllocationRequestTime]) VALUES ('3179543',2014,'19','1','2','107-08-106_421','3','6','380','0','0.0000000','0.0000000','44.4144000','24.5186000','
here dataReturned: الدوادمي ','421-0-0','R','0','0',GETDATE(),0,GETDATE())
here dataReturned:
here dataReturned: INSERT INTO RPTProblemTypes_GVT ([TaskYear] ,[TaskCode] ,[pbTypeId] ,[ReportDate] ,[INSTANCENAME] ,[PK] ,[ParentId] ) VALUES ('2015','3179543','754','Sep 11 2015 11:11AM','1-1086','380-1','1086')
正如您所看到的,我只对检索标记QUERY
感兴趣,但输出错误!标记值正在拆分(即foundCharacters
方法在同一个标记上被多次调用)请有人帮我解决此问题。
我注意到的是,只有当标签中有阿拉伯字母时才会发生这种情况。!!!
谢谢
答案 0 :(得分:0)
对于任何遇到NSXMParser困难的人,使用SWXMLHash这显然是一种简单明了的方法
https://github.com/drmohundro/SWXMLHash
我最终做了这样的事情:
let xml = SWXMLHash.lazy(ReturnedData)
let Queries = (xml["ROWS"]["ROW"].all.map { elem in elem["QUERY"].element!.text;})
for Query in Queries
{
print (Query)
}