2013-05-27 106 views
-1

獲取數據我是新來的下面的代碼我電話programming.Using我得到響應形式web服務無法解析Web服務在iphone

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>&lt;?xml version="1.0" encoding="utf-8"?&gt; 
&lt;DataSet&gt; 
    &lt;xs:schema id="Result" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"&gt; 
    &lt;xs:element name="Result" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"&gt; 
     &lt;xs:complexType&gt; 
     &lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt; 
      &lt;xs:element name="Table"&gt; 
      &lt;xs:complexType&gt; 
       &lt;xs:sequence&gt; 
       &lt;xs:element name="SystemDateTime" type="xs:dateTime" minOccurs="0" /&gt; 
       &lt;xs:element name="CafeDateTime" type="xs:dateTime" minOccurs="0" /&gt; 
       &lt;xs:element name="LicenseNumber" type="xs:string" minOccurs="0" /&gt; 
       &lt;xs:element name="MachineIP" type="xs:string" minOccurs="0" /&gt; 
       &lt;xs:element name="TotalClients" type="xs:int" minOccurs="0" /&gt; 
       &lt;xs:element name="IdleClients" type="xs:int" minOccurs="0" /&gt; 
       &lt;xs:element name="BusyClients" type="xs:int" minOccurs="0" /&gt; 
       &lt;xs:element name="DummyIdle" type="xs:int" minOccurs="0" /&gt; 
       &lt;xs:element name="DummyBusy" type="xs:int" minOccurs="0" /&gt; 
       &lt;xs:element name="PCUsageSessions" type="xs:int" minOccurs="0" /&gt; 
       &lt;xs:element name="PCUsageMinutes" type="xs:int" minOccurs="0" /&gt; 
       &lt;xs:element name="SessionMessage" type="xs:string" minOccurs="0" /&gt; 
       &lt;xs:element name="CreatedBy" type="xs:int" minOccurs="0" /&gt; 
       &lt;xs:element name="CreatedDate" type="xs:dateTime" minOccurs="0" /&gt; 
       &lt;xs:element name="Cybercafe" type="xs:string" minOccurs="0" /&gt; 
       &lt;xs:element name="Email" type="xs:string" minOccurs="0" /&gt; 
       &lt;xs:element name="AdminPassword" type="xs:string" minOccurs="0" /&gt; 
       &lt;xs:element name="MajorVersion" type="xs:short" minOccurs="0" /&gt; 
       &lt;xs:element name="MinorVersion" type="xs:short" minOccurs="0" /&gt; 
       &lt;xs:element name="SalesAmount" type="xs:decimal" minOccurs="0" /&gt; 
       &lt;xs:element name="PurchasesAmount" type="xs:decimal" minOccurs="0" /&gt; 
       &lt;xs:element name="Revision" type="xs:int" minOccurs="0" /&gt; 
       &lt;xs:element name="CZLicense" type="xs:string" minOccurs="0" /&gt; 
       &lt;/xs:sequence&gt; 
      &lt;/xs:complexType&gt; 
      &lt;/xs:element&gt; 
     &lt;/xs:choice&gt; 
     &lt;/xs:complexType&gt; 
    &lt;/xs:element&gt; 
    &lt;/xs:schema&gt; 
    &lt;diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"&gt; 
    &lt;Result&gt; 
     &lt;Table diffgr:id="Table1" msdata:rowOrder="0"&gt; 
     &lt;SystemDateTime&gt;2012-09-20T00:00:00-06:00&lt;/SystemDateTime&gt; 
     &lt;CafeDateTime&gt;2012-09-18T11:01:54-06:00&lt;/CafeDateTime&gt; 
     &lt;LicenseNumber&gt;cc&lt;/LicenseNumber&gt; 
     &lt;MachineIP&gt;192.168.13.167&lt;/MachineIP&gt; 
     &lt;TotalClients&gt;0&lt;/TotalClients&gt; 
     &lt;IdleClients&gt;0&lt;/IdleClients&gt; 
     &lt;BusyClients&gt;0&lt;/BusyClients&gt; 
     &lt;DummyIdle&gt;0&lt;/DummyIdle&gt; 
     &lt;DummyBusy&gt;0&lt;/DummyBusy&gt; 
     &lt;PCUsageSessions&gt;0&lt;/PCUsageSessions&gt; 
     &lt;PCUsageMinutes&gt;0&lt;/PCUsageMinutes&gt; 
     &lt;SessionMessage&gt;LVCLPRLbSCTC&lt;/SessionMessage&gt; 
     &lt;CreatedBy&gt;1&lt;/CreatedBy&gt; 
     &lt;CreatedDate&gt;2012-09-20T00:00:00-06:00&lt;/CreatedDate&gt; 
     &lt;Cybercafe&gt;Cyber Cafe Name&lt;/Cybercafe&gt; 
     &lt;Email&gt;[email protected]&lt;/Email&gt; 
     &lt;AdminPassword&gt;admin1&lt;/AdminPassword&gt; 
     &lt;MajorVersion&gt;1&lt;/MajorVersion&gt; 
     &lt;MinorVersion&gt;0&lt;/MinorVersion&gt; 
     &lt;SalesAmount&gt;0.0000&lt;/SalesAmount&gt; 
     &lt;PurchasesAmount&gt;0.0000&lt;/PurchasesAmount&gt; 
     &lt;Revision&gt;0&lt;/Revision&gt; 
     &lt;CZLicense /&gt; 
     &lt;/Table&gt; 
    &lt;/Result&gt; 
    &lt;/diffgr:diffgram&gt; 
&lt;/DataSet&gt;</OnlineStatusResult></OnlineStatusResponse></soap:Body></soap:Envelope> 
響應

我沒有得到什麼是& lt和&gt在tag.I沒有得到開始標記和結束標記在這個response.and也在xml

而且usign下面的代碼我在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嘗試分析數據,但在這裏它不加載數據,如低於其empty.I想分析使用開始和結束元素的數據。

2013-05-27 13:55:13.179 NewC Newcafezee[1191:11303] iiiiii <NSXMLParser: 0x71c7060> 
+3

是否實現瞭解析器委託? –

+0

雅我已經實施代表 – Aslam

+0

/你在代表中的迴應是什麼? –

回答

0

NSXMLParser不解析數據到一個NSDictionary你,它會通過所有的元素循環,你需要得到你需要使用它的委託(NSXMLParserDelegate)的值。

有框架,如XMLReader(它實際上使用NSXMLParser),處理所有元素的解析成NSDictionaryNSArray(取決於XML是如何被格式化)。

NSLog(@" iiiiii %@",xmlParser);將顯示一個NSXMLParser對象的描述,並正確輸出(它不應該顯示解析的內容)。

關於&字符,這些字符編碼html characters。服務器上可能存在問題,因爲它返回了xml編碼的一部分,並且其中一部分被解碼。

編輯:

這需要一個的NSString類別添加,也可以事後調用此方法對您的字符串來自服務器的響應。

-(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:@"&quot;"]) { 
        [ret appendString:@"\""]; 
       } 
       else if([special isEqualToString:@"&#x27;"]){ 
        [ret appendString:@"'"]; 
       } 
       else if([special isEqualToString:@"&lt;"]){ 
        [ret appendString:@"<"]; 
       } 
       else if([special isEqualToString:@"&gt;"]){ 
        [ret appendString:@">"]; 
       } 
       else if([special isEqualToString:@"&amp;"]){ 
        [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]; 
} 
+0

我如何解析xml data.its不加載xmlparser – Aslam

+0

@Aslam什麼'[xmlParser解析];'返回?如果它返回'NO',還有'NSLog(@「%@」,[xmlParser parseError]);'。你需要解碼你的xml。 'xmlDecodedString'方法應該可以正常工作(在我的響應中更新),但是,如果可能的話,我建議應該從服務器修復部分編碼的xml問題。 –

+0

有了新面貌,xml沒有問題,零件被編碼,因爲它被設置爲元素的內容。你應該檢查'NSXMLParser'返回的錯誤 –