3

我需要一些帮助来解析这个响应。我认为它是 WSDL 并想检查“成功输入”字符串。下面是代码:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://example.com/reg/abc">
   <soapenv:Header/>
   <soapenv:Body>
      <sch:LoginResponse>
         <sch:Status code=200>Successfully Entered</sch:Status>
         <sch:Message>[CDATA[Successfully Entered]]</sch:Message>
      </sch:LoginResponse>
   </soapenv:Body>
</soapenv:Envelope>

编辑:下面是我实现的代码

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
   attributes: (NSDictionary *)attributeDict
{
    NSLog(@"START ELEMENT NAME ==> %@", elementName);
    if( [elementName isEqualToString:@"sch:Status"])
    {
        if(!soapResults)
        {
            soapResults = [[NSMutableString alloc] init];
        }
        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
{
    NSLog(@"END ELEMENT NAME ==> %@", elementName);
    if( [elementName isEqualToString:@"sch:Status"])
    {
        NSLog(@"Found ==> %@", soapResults);
        [soapResults setString:@""];
        elementFound = FALSE;
    }
}

打印日志显示 uptosch:LoginResponse而不是元素名称sch:Status

4

1 回答 1

2

根据我们的聊天,问题出在:

<sch:Status code=200>Successfully Entered</sch:Status>

何时code=200更改为code="200"正确解析。

换句话说,XML 的格式不正确。属性应该在数据周围有一个单'引号 ( ) 或双引号 ( ")。(参见W3C XML 推荐

NSXMLParserAttributeNotStartedError此外,当保持不变时,委托会检测 XML 中的错误:

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"Error: %@", parseError.localizedDescription);
}

如果必须以这种方式检索XML ,则在解析之前,您可以使用它用引号将其括起来以使其有效,但是您需要为每个可能的情况执行此操作code=###

yourXMLString = [yourXMLString stringByReplacingOccurrencesOfString:@"code=200" withString:@"code=\"200\""];
于 2013-05-18T09:27:58.523 回答