问题与iPhone
XML解析当我从网络服务接收数据我NSMutableData充满了以下XML:问题与iPhone
<?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><GetWeatherResponse xmlns="http://www.webserviceX.NET"><GetWeatherResult><?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>BERLIN MUNICIPAL AIRPORT, NH, United States (KBML) 44-35N 71-11W 345M</Location>
<Time>Oct 19, 2010 - 03:52 AM EDT/2010.10.19 0752 UTC</Time>
<Wind> Calm:0</Wind>
<Visibility> 10 mile(s):0</Visibility>
<SkyConditions> clear</SkyConditions>
<Temperature> 23.0 F (-5.0 C)</Temperature>
<DewPoint> 21.0 F (-6.1 C)</DewPoint>
<RelativeHumidity> 91</RelativeHumidity>
<Pressure> 29.83 in. Hg (1010 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather></GetWeatherResult></GetWeatherResponse></soap:Body></soap:Envelope>
所以,当我搜索“CurrentWeather”解析器不能找到它,因为& QT, & lt等。如何解决我的NSMutableData有正常值(<,>等)?
完整代码
#import "DemoWebServiceConsumeViewController.h"
@implementation DemoWebServiceConsumeViewController
@synthesize cityName;
@synthesize activityIndicator;
@synthesize location;
- (IBAction) hideKeyboard{
[cityName resignFirstResponder];
}
- (IBAction) buttonClicked: (id)sender{
[cityName resignFirstResponder];
NSString *soapMsg =
[NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<GetWeather xmlns=\"http://www.webserviceX.NET\">"
"<CityName>%@</CityName>"
"<CountryName>%@</CountryName>"
"</GetWeather>"
"</soap:Body>"
"</soap:Envelope>", cityName.text, @"united states"
];
NSLog(soapMsg);
NSURL *url = [NSURL URLWithString:
@"http://www.webservicex.com/globalweather.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
//---set the headers---
// here copy method name to be called SOAP Action read from WS description
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8"
forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://www.webserviceX.NET/GetWeather"
forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
[activityIndicator startAnimating];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
}
-(void) connection:(NSURLConnection *) connection
didReceiveResponse:(NSURLResponse *) response {
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection
didReceiveData:(NSData *) data {
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection
didFailWithError:(NSError *) error {
[webData release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(@"DONE READING WEATHER WEB SERVICE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc]
initWithBytes: [webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
//---shows the XML---
NSLog(theXML);
[theXML release];
[activityIndicator stopAnimating];
if (xmlParser)
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
[connection release];
[webData release];
}
//---when the start of an element is found---
-(void) parser:(NSXMLParser *) parser
didStartElement:(NSString *) elementName
namespaceURI:(NSString *) namespaceURI
qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict {
NSLog(elementName);
if([elementName isEqualToString:@"GetWeatherResult"])
{
if (!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
elementFound = YES;
}
else if([elementName isEqualToString:@"Location"])
{
elementFound = YES;
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFound)
{
[soapResults appendString: string];
}
}
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"GetWeatherResult"])
{
NSLog(soapResults);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Current Temperature!"
message:soapResults
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[soapResults setString:@""];
elementFound = FALSE;
}
}
@end
我已经编辑您的问题,所以它显示了我认为是你的意思进行粘贴。它看起来像Web服务将整个XML文件封装为另一个XML标签内的字符串。因此,您需要做的是将<GetWeatherResult>
XML标记的全部内容作为单个字符串获取。我认为NSXMLParser会自动替换正确的字符来代替>
等。
得到该字符串后,您需要将其传递到另一个NSXMLParser以解析其内容。
我添加了新的NSXMLParser * weatherXmlParser;在头文件中。在didEndElement方法中,当我得到有效的XML时,我将它存储在NSMutableString * soapResults;变量。并尝试解析它:if(weatherXmlParser) \t \t { \t \t \t [weatherXmlParser release]; \t \t} \t \t weatherXmlParser = [[NSXMLParser alloc] initWithData:soapResults]; \t \t [weatherXmlParser setDelegate:self]; \t \t \t [weatherXmlParser setShouldResolveExternalEntities:YES]; \t \t [weatherXmlParser parse];但我得到以下错误“抛出'NSException'实例后终止调用 程序接收到的信号:”SIGABRT“。”。 – 1110 2010-10-19 11:02:28
您需要将soapResults转换为NSData。 。 – JeremyP 2010-10-19 11:09:41
我很抱歉因为我无聊:(我说 “的NSData * WDATA = [soapResults dataUsingEncoding:NSUTF8StringEncoding]; \t \t \t \t \t \t的NSLog(WDATA); ”,但我收到新的错误:“所谓的终止抛出一个'NSException'的实例后 程序接收到的信号:“SIGABRT”。 “。并且在这个解析方法没有被调用之后 – 1110 2010-10-19 11:32:53
你可以发布你做这种转换的代码部分吗?错误可能是由于编码集不正确,但没有代码很难说。 – bontoJR 2010-10-19 08:39:36
确实;我们需要先看看您如何检索数据,然后才能提供帮助。 – 2010-10-19 09:03:45