问题描述
我是手机编程的新手。使用下面的代码我收到回复表格webservice
I am new to i phone programming.Using below code i getting response form webservice
NSString *soapMessage=[NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<OnlineStatus xmlns=\"http://tempuri.org/\">\n"
"<CafeName>Cyber Cafe Name</CafeName>\n"
"<FromDate>2012-03-01</FromDate>\n"
"<ToDate>2013-05-03</ToDate></OnlineStatus>\n"
"</soap:Body>\n"
"</soap:Envelope>"];
NSLog(@"%@",soapMessage);
NSURL *url = [NSURL URLWithString:@"http://www.ebidmanagerdemo.com/gjHouseOnline/xml/XMLDataService.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/OnlineStatus" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection) {
webData = [NSMutableData data];
}
else {
NSLog(@"theConnection is NULL");
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
NSLog(@"%@",msgLength);
}
}
我收到以下格式的回复
2013-05-27 13:55:10.028 NewC Newcafezee[1191:11303] <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<OnlineStatus xmlns="http://tempuri.org/">
<CafeName>Cyber Cafe Name</CafeName>
<FromDate>2012-03-01</FromDate>
<ToDate>2013-05-03</ToDate></OnlineStatus>
</soap:Body>
</soap:Envelope>
2013-05-27 13:55:13.170 NewC Newcafezee[1191:11303] DONE. Received Bytes: 18345
2013-05-27 13:55:13.170 NewC Newcafezee[1191:11303] getting in xml <?xml version="1.0" encoding="utf-8"?><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><OnlineStatusResponse xmlns="http://tempuri.org/"><OnlineStatusResult><?xml version="1.0" encoding="utf-8"?>
<DataSet>
<xs:schema id="Result" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Result" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="SystemDateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="CafeDateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="LicenseNumber" type="xs:string" minOccurs="0" />
<xs:element name="MachineIP" type="xs:string" minOccurs="0" />
<xs:element name="TotalClients" type="xs:int" minOccurs="0" />
<xs:element name="IdleClients" type="xs:int" minOccurs="0" />
<xs:element name="BusyClients" type="xs:int" minOccurs="0" />
<xs:element name="DummyIdle" type="xs:int" minOccurs="0" />
<xs:element name="DummyBusy" type="xs:int" minOccurs="0" />
<xs:element name="PCUsageSessions" type="xs:int" minOccurs="0" />
<xs:element name="PCUsageMinutes" type="xs:int" minOccurs="0" />
<xs:element name="SessionMessage" type="xs:string" minOccurs="0" />
<xs:element name="CreatedBy" type="xs:int" minOccurs="0" />
<xs:element name="CreatedDate" type="xs:dateTime" minOccurs="0" />
<xs:element name="Cybercafe" type="xs:string" minOccurs="0" />
<xs:element name="Email" type="xs:string" minOccurs="0" />
<xs:element name="AdminPassword" type="xs:string" minOccurs="0" />
<xs:element name="MajorVersion" type="xs:short" minOccurs="0" />
<xs:element name="MinorVersion" type="xs:short" minOccurs="0" />
<xs:element name="SalesAmount" type="xs:decimal" minOccurs="0" />
<xs:element name="PurchasesAmount" type="xs:decimal" minOccurs="0" />
<xs:element name="Revision" type="xs:int" minOccurs="0" />
<xs:element name="CZLicense" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<Result>
<Table diffgr:id="Table1" msdata:rowOrder="0">
<SystemDateTime>2012-09-20T00:00:00-06:00</SystemDateTime>
<CafeDateTime>2012-09-18T11:01:54-06:00</CafeDateTime>
<LicenseNumber>cc</LicenseNumber>
<MachineIP>192.168.13.167</MachineIP>
<TotalClients>0</TotalClients>
<IdleClients>0</IdleClients>
<BusyClients>0</BusyClients>
<DummyIdle>0</DummyIdle>
<DummyBusy>0</DummyBusy>
<PCUsageSessions>0</PCUsageSessions>
<PCUsageMinutes>0</PCUsageMinutes>
<SessionMessage>LVCLPRLbSCTC</SessionMessage>
<CreatedBy>1</CreatedBy>
<CreatedDate>2012-09-20T00:00:00-06:00</CreatedDate>
<Cybercafe>Cyber Cafe Name</Cybercafe>
<Email>[email protected]</Email>
<AdminPassword>admin1</AdminPassword>
<MajorVersion>1</MajorVersion>
<MinorVersion>0</MinorVersion>
<SalesAmount>0.0000</SalesAmount>
<PurchasesAmount>0.0000</PurchasesAmount>
<Revision>0</Revision>
<CZLicense />
</Table>
</Result>
</diffgr:diffgram>
</DataSet></OnlineStatusResult></OnlineStatusResponse></soap:Body></soap:Envelope>
我没有得到标签末尾的& lt和& gt。我不是在此响应中获取开始标记和结束标记。还在xml中
I am not getting what is < and > at end of tag.I am not getting start tag and end tag in this response.and also in xml
并在下面的代码中使用我正在尝试解析数据,但这里它的加载数据不在 NSXMLParser
And usign below code i am trying parse data but here its not loading data in NSXMLParser
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@" getting in xml %@",theXML);
xmlParser = [[NSXMLParser alloc]initWithData:webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
NSLog(@" iiiiii %@",xmlParser);
}
它在 NSXMLParser中显示
就像它下面的空。我想用开始和结束元素解析数据。
Its show in NSXMLParser
like below its empty.I want to parse data using start and end elements.
2013-05-27 13:55:13.179 NewC Newcafezee[1191:11303] iiiiii <NSXMLParser: 0x71c7060>
推荐答案
NSXMLParser
不会为你解析数据到 NSDictionary
,它会循环遍历所有元素,你需要使用你需要的值来获取它的委托( NSXMLParserDelegate
)。
NSXMLParser
doesn't parse the data into a NSDictionary
for you, it'll loop through all the elements, and you'll need to get the values that you need using its delegate (NSXMLParserDelegate
).
有框架,例如(实际上使用 NSXMLParser
),用于处理将所有元素解析为 NSDictionary
或 NSArray
(取决于格式化xml的方式)。
There are frameworks, such as XMLReader (which actually uses NSXMLParser
), that handles the parsing of all elements into a NSDictionary
or NSArray
(depending on how the xml is formatted).
NSLog(@iiiiii%@,xmlParser);
将显示 NSXMLParser
对象的描述,以及正确输出(它不应该显示解析的内容)。
NSLog(@" iiiiii %@",xmlParser);
will show the description of a NSXMLParser
object, and outputs correctly (it's not supposed to show the parsed content).
关于&
字符,这些字符是编码的。服务器上可能存在问题,因为它返回部分xml编码,部分解码。
Regarding the &
characters, those are encoded html characters. There may be an issue on the server, since it returns part of the xml encoded, and part of it decoded.
编辑:
这需要在NSString类别中添加,然后您可以从服务器响应中对字符串调用此方法。
This needs to be added in a NSString category, and you can afterwards call this method on your string from the server response.
-(NSString *)xmlDecodedString{
NSMutableString *ret = [[NSMutableString alloc] initWithCapacity:self.length];
NSRange specialRange = NSMakeRange(NSNotFound, NSNotFound);
// using %c causes some issues
NSRange decRange = NSMakeRange(0, NSNotFound);
for (NSUInteger i=0; i<[self length]; i++) {
char c = [self characterAtIndex:i];
if (specialRange.location==NSNotFound) {
// search for the start of a special 'character' (starts with '&')
if (c == '&'){
if (i>decRange.location) {
decRange.length=i-decRange.location;
[ret appendString:[self substringWithRange:decRange]];
decRange = NSMakeRange(i+1, NSNotFound);
}
specialRange.location=i;
}
}
else{
// search for the end of the special 'character' (ends with ';')
if (c == ';'){
decRange = NSMakeRange(i+1, NSNotFound);
specialRange.length=i-specialRange.location+1;
NSString *special = [self substringWithRange:specialRange];
specialRange = NSMakeRange(NSNotFound, NSNotFound);
if ([special isEqualToString:@"""]) {
[ret appendString:@"\""];
}
else if([special isEqualToString:@"'"]){
[ret appendString:@"'"];
}
else if([special isEqualToString:@"<"]){
[ret appendString:@"<"];
}
else if([special isEqualToString:@">"]){
[ret appendString:@">"];
}
else if([special isEqualToString:@"&"]){
[ret appendString:@"&"];
}
else{
DLogWarn(@"WARNING: unhandled special entity %@", special);
[ret appendString:special];
}
}
}
}
if (self.length>decRange.location) {
decRange.length=self.length-decRange.location;
[ret appendString:[self substringWithRange:decRange]];
}
return [ret autorelease];
}
这篇关于无法解析在iphone中获取数据的web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!